Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError

Every thread created in a Java program has its own stack space. The stack space used is not allocated from the heap. Infact if you look at the OS report on the memory used by your JVM, you may notice that it is more than what -Xmx parameter specifies. This is because, beside other things, memory is used for the thread stacks too. And this memory is not included in the heap specified by the -Xms and -Xmx switches.

The thread stack is used to push stacks frames in nested method calls. If the nesting is so deep that the thread runs out of space, the thread dies with a StackOverflowError.

The default thread stack size varies with JVM, OS and environment variables. A typical value is 512k. It is generally larger for 64bit JVMs because references are 8 bytes rather than 4 bytes in size. This means that if your app uses 150 threads, 75MB will be used for thread stacks. In some environments the defaults stack may be as large as 2MB. With a large number of threads, this can consume a significant amount of memory which could otherwise be used by your application or OS.

In most applications, 128k happens to be enough for the stack. What you really need to do is adjust and observe. If you don’t see your app running out of stack space, use the -Xss JVM parameter to specify a smaller stack (-Xss128k).

Note that it is entirely possible that your OS rounds up values for stack size specified by your -Xss parameter. Watch out for that.

Using the program below, you can see how stack space is used up by methods with varying number of arguments. You’ll get the StackOverflowError with fewer nested method calls for a smaller stack. You could try adjusting the number of arguments and even the type of arguments for different behaviour.

By adjusting the stack size, and keeping the code the same, you can see the JVM dying at different points in the recursive call.



Tags: ,
This entry was posted on Thursday, February 17th, 2011 at 5:19 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

9 Responses to “Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError”

  1. Ilias Tsagklis

    Hi Onkar,

    Very interesting blog! Is there an email address I can contact you in private?

  2. Hareesh

    Hi Nice Answers… Can I get in touch with you Onkar?

  3. vikas

    Short and precise explanation. Thank you for the info …

  4. Bappaditya

    Very well explained….

  5. Kondal

    Nice write up!!

  6. srikanth

    Thanks for explaining in short. very good article and very useful

  7. Binh Thanh Nguyen

    Thanks, nice post

  8. What is the difference between -Xss and -XX:ThreadStackSize? | 我爱源码网

    […] (groovy) application. For the Hotspot Oracle VM, I know that there are two parameters doing that (-Xss and […]

  9. Sekhar

    Hi Onkar,

    I am having some issue with this -Xss parameter of JVM, I am using 1.7.72 jre in Linux & windows. I am using -Xss as 128K which is working in almost all windows family. In Ubuntu we faced strange issue. The same configurations are working in Ubuntu 11 in 32 & 64 bits. In Ubuntu-14 64 bit machine we are getting an error saying that minimum Xss value should be 228K. If this is JRE wide parameter then in different OS flavors why we will get an error? is this parameter will play vital role in various OS flavors?

Leave a Reply

Your comment