Version 13

    Why Do I Get OutOfMemoryException When My Operating System Is Unable to Create More Threads?

     

    What is Thread Stack

    Thread Stack is the memory that is used by each thread to push/pop functions and variables used for function calls(this is also called the call stack).  If you have very long calls(possibly recursive), you could take up a relatively large amount of stack space and you may run into Stack overflows.(if you get this with a small Xss, raise it a little). Objects are not pushed into this memory usually.  The address of the object in heap is usually pushed into the call stack.  So don't allocate Thread Stack Size based on the size of your objects.  It's usually a function of how deep the call stack is.

     

    Thread stack space comes out of the process space and is not allocated from Heap.  Because the heap also comes out of the process space, allocating heap vs thread stack is a balancing act.  If you allocate to much heap, you can run out of process space for your threads.  If you allocate too much Thread Stack or if you have too many threads grabbing process memory, when your heap grows, it may not be able to allocate up to it's Xmx for memory and you get the error "OutOfMemoryError: heap allocation failed".

     

    Please keep in mind that process space is based on the Operating system.  For instance, you may have 4 GB of physical memory on a Windows machine, but only 2GB can be allocated for a process, due to the overhead of the Operating system.  When you add Java, then you only really have 1.5 GB of process space left to allocate.  If you allocate 1.5 GB of heap space you will most likely run out of process space trying to allocate Thread stack for new threads.

     

    A good ad hock formula for computing how much process space you will take up is..

    TotalProcessSpace = .5GB(java overhead) + Heap + (ThreadsThreadStackSize)

     

    -


    OutOfMemoryException: unable to create new native thread

    This exception is caused by one of two things.

     

    1. A common issue 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.

    2. Another common issue is that the default stack size on recent vms with the -server option is probably too large.  While the -server option should be specified with JBoss, it is prboably wise to reduce the stack size (allocated per thread) for most applications.  (On Solaris 32bit the default is 512k, 64bit is 1024k!  The default for 32 bit linux is also 512k in recent VMs)  Most JBoss applications are fine with a stack size of 128k.  The size is controlled with the -Xss option on most VMs, however on solaris this can only increase the size.  you must pass -XX:ThreadStackSize=128 to reduce it.

     

    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.  Plus NPTL was MUCH faster (a couple orders of magnitude).  See LinuxThreadingModel for details.

     

    -


     

    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.

     

     

    Referenced by: