Linux 中 Java VM 可以支持多少个线程?
我编写了线程池类,引用 http://www.informit.com /articles/article.aspx?p=30483&seqNum=5
环境:Windows7 4 cp
在Windows 7中用70,000个线程执行我的程序,在JDK 1.5下成功执行。未使用 vm 参数。
我尝试在 Linux 企业版中使用 5,000 个线程执行相同的代码,该版本位于具有 4GB 基本内存的 Virtual Box 下。带有 vm 参数 -xms512m -xmx1024m。它执行到 2156 个线程并抛出异常
Exception in thread "main" java.lang.OutOfMemoryError:无法创建新的本机线程 在java.lang.Thread.start0(本机方法) 在 java.lang.Thread.start(Thread.java:597) 在 testthreadpool.ThreadPool。(ThreadPool.java:38) 在 testthreadpool.TestThreadPool.main(TestThreadPool.java:16)
但相同的代码在 windows7 中运行完美。
我可以知道为什么会出现这个错误吗?这个java代码需要1GB内存来运行5,000个线程吗?...
我的实际要求是持有一个具有10,000个工作线程的线程池。
I wrote Thread pool class referring http://www.informit.com/articles/article.aspx?p=30483&seqNum=5
Environment: Windows7 4 cp
Executed my program with 70,000 Thread in Windows 7, under JDK 1.5 it went through successfully. Not used vm arguments.
The same Code i tried to execute with 5,000 Thread in Linux enterprise edition which is under Virtual Box with 4GB base memory. with vm arguments -xms512m -xmx1024m. It executes till 2156 threads and throws an exception
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:597)
at testthreadpool.ThreadPool.(ThreadPool.java:38)
at testthreadpool.TestThreadPool.main(TestThreadPool.java:16)
But the same code run perfectly in windows7.
May i know why this error occurs. Does this java code need 1GB memory to run Just 5,000 Threads?...
My actual requirement is to hold a ThreadPool with 10,000 Workthread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为你需要重新审视你的要求。这绝不是一个好主意,而且对性能来说是灾难性的。
I think you need to revisit your requirement. That in no way is a good idea, and is catastrophic to performance.
正如 @Yann 指出的那样,使用 10,000 个线程是一个非常糟糕的主意......除非您拥有一台具有数千个核心的机器。您应该认真审视您的应用程序设计。
短期内,尝试使用
-Xss...
JVM 参数调整默认线程堆栈大小。另请注意,堆栈不是在堆内存中分配的,因此您的-Xms512m -Xmx1024m
选项不会为堆栈保留空间。相反,它保留了不能用于堆栈的空间。最后,可能还有其他因素(线程堆栈的内存除外)会限制应用程序可以创建的线程数量。
As @Yann points out, using 10,000 threads is a really bad idea ... unless you have a machine with thousands of cores. You should take a serious look at your application design.
In the short term, try tuning the default thread stack size with the
-Xss...
JVM parameter. Also note that stacks are not allocated in heap memory, so your-Xms512m -Xmx1024m
option is not reserving space for stacks. On the contrary, it is reserving space that cannot then be used for stacks.Finally, there may be other things (other than memory for thread stacks) that will limit the number of threads that your application can create.
线程需要一个堆栈,该堆栈必须具有初始大小。对于线程,初始堆栈大小默认为堆栈大小资源限制,如
ulimit -s
所示,但可以通过调用pthread_attr_setstacksize()
进行更改。 (请参阅另一个SO问题)。Threads require a stack, that has to have an initial size. For threads, the initial stack size is by default the stack size reource limit, as shown by
ulimit -s
, but can be changed by a call topthread_attr_setstacksize()
. (See this other SO question).你是64位的吗?
不要指望 32 位机器能够运行大量线程。您可能还希望调整堆栈大小。启动大量线程会占用大量堆栈内存,除非您能够容忍较小的堆栈,否则您无法解决这个问题。
在 x86_64 上检查,Linux 似乎默认为 8M 堆栈,这意味着 1k 线程占用 8G 堆栈,所以你真的要小心这一点。
Are you on 64-bit?
Don't expect a 32-bit machine to be able to run lots of threads. You may also wish to tweak the stack size. Starting lots of threads uses lots of memory for stacks, and you can't get around that unless you can tolerate smaller stacks.
Checking on x86_64, Linux seems to default to 8M stacks, which means that 1k threads takes 8G stack, so you really want to be careful with that.