问题描述
我有一个循环,产生了很多线程.这些线程除其他外包含2个(大量)StringBuilder对象.然后这些线程运行并执行其操作.
I have a loop that spawns a lot of threads. These threads contains, among other things, 2 (massive) StringBuilder objects. These threads then run and do their thing.
但是,我注意到经过一定数量的线程处理后,出现了奇怪的崩溃.我知道这是因为有了这些StringBuilder,因为当我减少它们的初始容量时,我可以启动更多的线程.现在,对于这些StringBuilder,它们是在线程对象的构造函数中创建的:
However, I noticed that after a certain amount of threads, I get strange crashes. I know this is because of these StringBuilder, because when I reduce their initial capacity, I can start a lot more threads. Now for these StringBuilders, they are create like this in the constructor of the thread object:
StringBuilder a =新的StringBuilder(30000);
StringBuilder b =新的StringBuilder(30000);
StringBuilder a = new StringBuilder(30000);
StringBuilder b = new StringBuilder(30000);
它通常崩溃的点是大约550个线程,这导致多于62MB.结合程序的其余部分,正在使用的内存很可能是64MB,我在网上读到的是JVM内存分配池的减少大小.我不知道这是不是真的.
The point where it generally crashes is around 550 threads, which results in a little bit more than 62MB. Combined with the rest of the program the memory in use is most likely 64MB, which I read online somewhere was the defaulf size of the JVM memory allocation pool. I don't know whether this is true or not.
现在,我在做错什么吗,由于某种原因,由于设计原因,我以错误的方式分配了内存?还是这是唯一的方法,我应该告诉JVM增加其内存池吗?还是完全其他的东西?
Now, is there something I am doing wrong, that somehow because of the design, I am allocating memory the wrong way? Or is this the only way and should I tell the JVM to increase its memory pool? Or something else entirely?
另外,请不要告诉我设置较低的容量,我知道这些StringBuilder会在需要时自动增加容量,但是我想对此问题有一个解决方案.
Also, please do not tell me to set a lower capacity, I know these StringBuilders automatically increase their capacity when needed, but I would like to have a solution to this problem.
推荐答案
使用-Xmx
JVM选项来增加Java的最大堆大小.
Use the -Xmx
JVM option to increase the Java maximum heap size.
这篇关于Java内存分配限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!