{"id":11,"date":"2007-09-06T00:00:00","date_gmt":"2007-09-06T00:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=11"},"modified":"2007-09-06T00:00:00","modified_gmt":"2007-09-06T00:00:00","slug":"How to fix \"java.lang.OutOfMemoryError: unable to create new native thread\"","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=11","title":{"rendered":"How to fix &#8220;java.lang.OutOfMemoryError: unable to create new native thread&#8221;"},"content":{"rendered":"<p><DIV><br \/>\n<DIV class=posthead>I recently came across this exception on a couple of java systems that use many threads&nbsp; <EM>java.lang.OutOfMemoryError: unable to create new native thread<\/EM>. The strange thing was that the JVM had been assigned a lot of memory (1.5GB) and that it had at least half the memory available.&nbsp;<A href=\"http:\/\/bordoni.dyndns.org\/blog\/\">Michele<\/A>&nbsp;found <A href=\"http:\/\/jroller.com\/page\/rreyelts\/20040909\">this article<\/A>&nbsp;that points out that <STRONG>the more memory you give to the JVM the more likely you are to get java.lang.OutOfMemoryError: unable to create new native thread<\/STRONG> exceptions when you have many threads.<\/DIV><br \/>\n<DIV class=postbody><br \/>\n<P>Which makes perfect sense when you think about it. Each 32 bit process on Windows has 2GB &#8220;available&#8221; memory as 2GB is reserved to Windows. In my case the JVM grabbed 1.5 GB leaving 500MB. Part of the 500MB&nbsp;was used to map system dlls etc in memory so less than 400 MB&nbsp;was left. Now to the crucial point: When you create a thread in java it creates a Thread object in the JVM memory but it also creates a operating system thread. The operating system creates the thread with a thread stack in the 400MB that is left, <U>not<\/U> in the 1.5 GB allocated in the JVM. Java 1.4 uses a default stack size of 256kb but Java 1.5 uses a 1MB stack per thread. So, in the 400MB left to process I could only generate ~400 threads. Absurd but true: <STRONG>to create more threads you have to reduce the memory allocated to the JVM.<\/STRONG> Another option is to host the JVM in your own process using JNI.<\/P><br \/>\n<P>This formula gives a decent estimate for the number of threads you can create:&nbsp;<BR><EM>(MaxProcessMemory &#8211; JVMMemory &#8211; ReservedOsMemory) \/ (ThreadStackSize) = Number of threads<\/EM><\/P><br \/>\n<P>For Java 1.5 I get the following results assuming that the OS reserves about 120MB:<BR>1.5GB allocated to JVM: (2GB-1.5Gb-120MB)\/(1MB) = ~380 threads<BR>1.0GB allocated to JVM: (2GB-1.0Gb-120MB)\/(1MB) = ~880 threads<\/P><br \/>\n<P>Java 1.4 uses 256kb for the thread stack which lets you create a lot more threads:<BR>1.5GB allocated to JVM: ~1520 threads<BR>1.0GB allocated to JVM: ~3520 threads<\/P><br \/>\n<P>I have not tried the <A href=\"http:\/\/www.microsoft.com\/whdc\/system\/platform\/server\/PAE\/PAEmem.mspx\">3GB&nbsp;switch<\/A>&nbsp;but it should in theory let you create more threads.<\/P><\/DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently came across this exception on a couple of java systems that use many threads&nbsp; java.lang.OutOfMemoryError: unable to create new native thread. The strange thing was that the JVM had been assigned a lot of memory (1.5GB) and that it had at least half the memory available.&nbsp;Michele&nbsp;found this article&nbsp;that points out that the more &hellip; <a href=\"https:\/\/www.strongd.net\/?p=11\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">How to fix &#8220;java.lang.OutOfMemoryError: unable to create new native thread&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11"}],"version-history":[{"count":0,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/11\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}