“java.lang.OutOfMemoryError:无法创建新的本机线程”在 64 位和 8 GB 的 Mac 上!
可能的重复:
在 4GB iMac OSX 10.6.3 Snow Leopard(32 位)上,Java 无法超过 2542 个线程
我正在我的 64 位、8 GB 内存的 Mac 上运行一个线程密集型应用程序。我尝试改变VM参数的选项(进入Eclipse和cmd行),但对于2500个线程,我总是得到“线程“main”java.lang.OutOfMemoryError中的异常:无法创建新的本机线程”。
当它崩溃时,我看到堆内存只有 100 MB...
出了什么问题?
Possible Duplicate:
Can't get past 2542 Threads in Java on 4GB iMac OSX 10.6.3 Snow Leopard (32bit)
I'm running a thread intensive application onto my Mac 64 bit with 8 Gbyte of memory. I have tried to vary the option of the VM arguments (into Eclipse and in cmd line) but for 2500 thread I always got "Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread".
When it crashes I saw that heap memory is only 100 Mbyte...
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最大线程数有限制,这与您实际拥有的内存量无关。请参阅这个问题。
There is a limit on the maximum number of threads which is independent of how much memory you actually have. See this question.
大多数 JVM 实现在创建数千个线程后都会遇到限制。虽然可能有一些方法可以解决这个问题(例如使用 -Xss 减小堆栈大小),但除非您有数千个处理器,否则没有太多理由使用数千个线程。
相反,您应该考虑使用更少的线程来提高效率。如果是 CPU 密集型,那么关闭您所拥有的处理器数量的方法将是理想的选择。如果它们在非 CPU 上被阻塞,那么比处理器更多的线程可能会有所帮助。
您可以使用
ExectorService
来管理线程池。例如:
Most implementations of the JVM run into limits after creating several thousand threads. While there might be ways to tune around this (like reducing the stack size with -Xss), there aren't many good reasons you'd use thousands of threads unless you had thousands of processors.
Instead you should consider using fewer threads for better efficiency. Something to close the number of processors that you have will be ideal if it's CPU intensive. If they're blocked on something non-CPU then more threads than processors could help.
You can use a
ExectorService
to manage a thread pool. E.g.: