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.

28 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. Timokalo

    Hi,
    Do you want send anonymous email with attachments?
    If yes, check this website, and have fun.

    Regards,
    T.

  9. Timokalo

    Hello,
    If you want send anonymous email with attachments check this website,
    and have fun.

    Regards,
    T.

  10. Timokalo

    Hi,
    Today I’ll share with you a service to send anonymous email with attachments
    Check the website, and don’t be evil.

    Have fun,
    T.

  11. Funionahiehab

    Niezla strona, komputery, informacje, programy i hosting …

  12. pizzeria 's-heerenberg

    Thanks for the good writeup. It in reality used to be a entertainment account it.
    Glance complex to far added agreeable from you! By the way, how could
    we communicate?

  13. cheap black womens riding boots

    cheap black womens riding boots

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  14. Alastair

    Alastair

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  15. homebase kitchens planner

    homebase kitchens planner

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  16. hay day twitter

    Why viewers still use to read news papers when in this technological world the whole thing is presented on net?

  17. Willkalo

    Hi,

    Today I’ll share with you the BEST service to send anonymous emails with attachments,

    Check the link anonymous email

    Have fun, and don’t be evil !

    W.

  18. istoikona.net

    istoikona.net

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  19. salopes asiatiques

    L’еnsemble de ces posts sont réellement plaisants

  20. sazbeanconsulting.com

    sazbeanconsulting.com

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  21. gabriellacoleman.org

    gabriellacoleman.org

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  22. Saraplorp

    Yo,

    Today I’ll share with you my favorite service to send anonymous emails with attachments,
    Test it for free from here : send anonymous emails
    Have fun, and give me your feedback !

    Regards,
    Sarah

  23. click the following post

    click the following post

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  24. babies

    babies

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  25. Askerarkadasi.net

    Askerarkadasi.net

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  26. porno hard gratuit

    Mince jjе pensais rédiger un petit article identique au
    votre

  27. im8275.com

    im8275.com

    Using -Xss to adjust Java default thread stack size to save memory and prevent StackOverflowError « Onkar Joshi’s blog

  28. apache cassandra 1.0.8 out of memory error unable to create new native threadnoflex.org

Leave a Reply

Your comment