title: Java Memory error: unable to create new native thread
body: <p>I get this error on my UNIX server, when running my java server:</p>
  
  <pre><code>Exception in thread "Thread-0" java.lang.OutOfMemoryError: unable to create new native thread
  at java.lang.Thread.start0(Native Method)
  at java.lang.Thread.start(Thread.java:640)
  at [... where ever I launch a new Thread ...]
  </code></pre>
  
  <p>It happens everytime I have about 600 threads running.</p>
  
  <p>I have set up this variable on the server:</p>
  
  <pre><code>$&gt; ulimit -s 128
  </code></pre>
  
  <p>What looks strange to me is the result of this command, which I ran when the bug occured the last time:</p>
  
  <pre><code>$&gt; free -m
                total       used       free     shared    buffers     cached
  Mem:          2048        338       1709          0          0          0
  -/+ buffers/cache:        338       1709
  Swap:            0          0          0
  </code></pre>
  
  <p>I launch my java server like this:</p>
  
  <pre><code>$&gt; /usr/bin/java -server -Xss128k -Xmx500m -jar /path/to/myJar.jar
  </code></pre>
  
  <p>My debian version:</p>
  
  <pre><code>$&gt; cat /etc/debian_version
  5.0.8
  </code></pre>
  
  <p>My java version:</p>
  
  <pre><code>$&gt; java -version
  java version "1.6.0_26"
  Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
  Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02, mixed mode)
  </code></pre>
  
  <p><strong>My question: I have read on Internet that my program should handle something like 5000 threads or so. So what is going on, and how to fix please ?</strong></p>
  
  <hr>
  
  <p>Edit: this is the output of <code>ulimit -a</code> when I open a shell:</p>
  
  <pre><code>core file size          (blocks, -c) unlimited
  data seg size           (kbytes, -d) unlimited
  scheduling priority             (-e) 0
  file size               (blocks, -f) unlimited
  pending signals                 (-i) 794624
  max locked memory       (kbytes, -l) 32
  max memory size         (kbytes, -m) unlimited
  open files                      (-n) 100000
  pipe size            (512 bytes, -p) 8
  POSIX message queues     (bytes, -q) 819200
  real-time priority              (-r) 0
  stack size              (kbytes, -s) 10240
  cpu time               (seconds, -t) unlimited
  max user processes              (-u) 794624
  virtual memory          (kbytes, -v) unlimited
  file locks                      (-x) unlimited
  </code></pre>
  
  <p>I run the script as a daemon from init.d, and this is what i run:</p>
  
  <pre><code>DAEMON=/usr/bin/java
  DAEMON_ARGS="-server -Xss128k -Xmx1024m -jar /path/to/myJar.jar"
  ulimit -s 128 &amp;&amp; ulimit -n 10240 &amp;&amp; start-stop-daemon -b --start --quiet --chuid $USER -m -p $PIDFILE --exec $DAEMON -- $DAEMON_ARGS \
      || return 2
  </code></pre>
  
  <hr>
  
  <p>Edit2: I have come across this stack overflow question with a java test for threads: <a href="https://stackoverflow.com/questions/763579/how-many-threads-can-a-java-vm-support">how-many-threads-can-a-java-vm-support</a></p>
  
  <pre><code>    public class DieLikeADog { 
          private static Object s = new Object(); 
          private static int count = 0; 
          public static void main(String[] argv){ 
              for(;;){ 
                  new Thread(new Runnable(){ 
                          public void run(){ 
                              synchronized(s){ 
                                  count += 1; 
                                  System.err.println("New thread #"+count); 
                              } 
                              for(;;){ 
                                  try { 
                                      Thread.sleep(100); 
                                  } catch (Exception e){ 
                                      System.err.println(e); 
                                  } 
                              } 
                          } 
                      }).start(); 
              } 
          } 
      } 
  </code></pre>
  
  <p>On my server, the program crashes after 613 threads. Now i'm certain this is not normal, and only related to my server configuration. Can anyone help please ?</p>
  
  <hr>
  
  <p>Edit 3:
  I have come across this article, and many others, explaining that <a href="http://gallium.inria.fr/~xleroy/linuxthreads/faq.html#D.10" rel="nofollow noreferrer">linux can't create 1000 threads</a>, but you guys are telling me that you can do it on your systems. I don't understand.</p>
  
  <p>I have also ran this script on my server: <a href="http://people.redhat.com/alikins/tuning_utils/thread-limit.c" rel="nofollow noreferrer">threads_limits.c</a> and the limit is around 620 threads.</p>
  
  <p>My website is now offline and this is the worst thing that could have happened to my project.
  I don't know how to recompile glibc and this stuff. It's too much work imo.</p>
  
  <p>I guess I should switch to windows server. Because none of the settings proposed on this page did make any change: The limit on my system is between 600 and 620 threads, no matter the program involved.</p>