Java very limited on max number of threads?

有些话、适合烂在心里 提交于 2019-12-20 19:08:47

问题


We have a small text box with 512Mb of ram. We wanted to see how many threads we can create in Java in this box. To our surprise, we can't create many. Essentially the minimum stack size you can set with -Xss is 64k. Simple math will tell you that 64*7000 will consume 430Mb so we were only able to get it up to around 7000 threads or so and then we encountered this error:

java.lang.OutOfMemoryError: unable to create new native thread. 

Is this the true limit with Java? Per 512Mb of ram we can only squeeze in 7k number of threads or so?


回答1:


Use asynchronous IO (java nio) and you'll don't need 7k threads to support 7k clients, a few threads for handling io (5?) will be enough.
Take a look at Netty ;)

One thread for each client is a really bad design.




回答2:


Once you create your 7k threads, you're not going to have any memory to do anything useful. Perhaps you should have a rethink about the design of your application?

Anyway, isn't 512Mb quite small? Perhaps you could provide a bit more information about your application or perhaps the domain?




回答3:


It's not the programming language, it's on the operating system level.

More reading about it, for Windows:

  • Does Windows have a limit of 2000 threads per process?
  • Pushing the Limits of Windows: Processes and Threads (by Mark Russinovich)



回答4:


Keep in mind that you will never be able to dedicate 100% of the RAM to running Java threads. Some RAM is used by the OS and other running applications, meaning you will never have the full 512 Mb available.




回答5:


You don't necessarily need one thread per client session. If you look at the way that a J2EE (or JavaEE) server handles multiple connections it uses a mixture of strategies including concurrency, queuing and swapping. Usually you can configure the maximum number of live concurrent instances and idle time-out values at deployment time to tune the performance of your application.




回答6:


Try setting the maximum memory allowed -Xmx to a lower value and see whether the thread count can be increased. In a project at work I could allocate around 2,5k threads with -Xmx512m and about 4k threads with -Xmx96m.

The bigger your heap the smaller your thread stack space (at least to my experience).



来源:https://stackoverflow.com/questions/3180988/java-very-limited-on-max-number-of-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!