4 Replies Latest reply on Mar 8, 2004 2:55 PM by Chad La Joie

    Native thread creation and Stack Overflow Exceptions: Soluti

    Chad La Joie Newbie

      This details the solution for the following two types exception on a Linux system:

      java.lang.OutOfMemoryError: unable to create new native thread
      This exception is caused by one of two things.

      1. The most common is that you reached the max number of user processes for the account that you're running JBoss on. You can see the limit by using "ulimit -u". Raising the limit should resolve this problem. Please note that there seems to be a hard coded ceiling of 1024 user processes, this may be a 2.4 kernel issues or a libc issue (both have such values in them, but I haven't done enough testing yet to tell which is causing this).

      2. Once you get number one solved you may see this error again, this time it does actually appear to be a memory issue. By default kernel 2.4 creates threads with a stack size of 2MB. There also seems to be some limit that only allows the use of 1GB of memory for stack allocations. So lots of threads could eat up lots of the 1GB the system has to work with. The fix for this one is use the -Xss jvm flag. The value of 1024k (so -Xss1024k) seems to work well. On the 2.4 kernel this allows some 300 threads to be created per vm.

      Kernel 2.6 + NPTL Note: As many people may know Kernel 2.6 allows for a new threading model known as NPTL. This new model really helps with the above problem. First it allows more then 1024 user processes. I was able to create about 2400 threads with a stack size of 128k. This compared to the approximate 500 I could create with a similar setup under the 2.4 kernel. Plus NPTL was MUCH faster (a couple orders of magnitude).

      StackOverflowException or
      Attempt to ungaurd stack red zone failed
      These errors are seen when you run out of stack space. Best fix for this one is to use the -Xss jvm flag and lower the amount of stack size each thread gets. Again, 1024k (-Xss1024k) seems to be a good number.