Java `OutOfMemoryError` when creating < 100 threads

前提是你 提交于 2019-11-29 03:54:34

You can be limit by max user processes, to know your limit use :

ulimit -u

To change the limit :

In /etc/security/limits.conf set :

user soft nproc [your_val] 
user hard nproc [your_val]

You may have to add some other config if it's not enough see this link.

Note : The OP found this bug report in fedora and centos which explains the limitations of editing /etc/security/limits.conf.

Your problem is probably related with JVM being unable to allocate stack memory for new threads. Ironically, this problem can be solved by decreasing heap space (-Xmx) and stack space (-Xss). Check here, for instance, for a good explanation: http://www.blogsoncloud.com/jsp/techSols/java-lang-OutOfMemoryError-unable-to-create-new-native-thread.jsp

Daniel Teply

It's not missing memory for your new threads, it's missing actual threads. The system is probably stopping you: there's a limit to the number of thread a user can create. You can query it that way:

cat /proc/sys/kernel/threads-max

Note that you might be impacted by other processes on the same machine, you they create many thread too. You might find this question useful: Maximum number of threads per process in Linux?

Just for clarification:

You provide a ServerSocket to the Thread. Do you send Data to that Socket? Maybe you store to much data within the Thread-Context. Tak a look for a pattern, where you store Streamdata in an byte[].

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