问题描述
我需要写10,000 x 30,000个字符.单个stringbuilder能否兼容所有字符,还是我应该考虑一个stringbuilder数组?我没有访问测试用例的权限,因此我无法亲自验证它.希望我能在这里找到答案.
I need to write 10,000 x 30,000 characters. will a single stringbuilder be able to acomodate all characters or should I think of an array of stringbuilders? I do not have access to the test cases, so I cannot actually verify it myself. Hope I will find the answer here.
谢谢.
我尝试使用循环添加10000 x 30000个字符.我遇到以下异常.
I tried to add 10000 x 30000 characters using a loop. I get the following exceptions.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2367)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:415)
at java.lang.StringBuilder.append(StringBuilder.java:132)
at Test.main(Test.java:19)
该"java堆空间"怎么办?
What to do with this "java heap space"?
推荐答案
该长度是一个整数,因此如果您有内存,则它最多可容纳2GChar(4GB).您将使用仅" 600MB(3亿个@每个字符2个字节).请小心,最终要制作多少个副本...即toString().
The length is an int so it should hold up to 2GChar (4GB) assuming you have the memory. You are going to use "only" 600MB (300 million @ 2 bytes per character). Just be careful how many copies you end up making... i.e. toString().
这篇关于stringbuilder可以容纳的最大字符数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!