Java `OutOfMemoryError` when creating < 100 threads

后端 未结 4 1850
醉梦人生
醉梦人生 2020-12-16 17:26

I\'ve been reading and testing and banging my head on the wall for over a day because of this error.

I have some Java code in a class called Listener t

相关标签:
4条回答
  • 2020-12-16 17:36

    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.

    0 讨论(0)
  • 2020-12-16 17:42

    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

    0 讨论(0)
  • 2020-12-16 17:43

    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[].

    0 讨论(0)
  • 2020-12-16 17:51

    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?

    0 讨论(0)
提交回复
热议问题