7

I have seen JVM option -Xss - What does it do exactly? this link, but my question is how is this option useful.

Because, if we set a very minimum limit to the -Xss value, maybe the threads are not going to work properly as it may throw stackOverflow error most of the times.

Why is there a 64k limit at least for this value?
How i got this 64k limit is when i was trying to configure the runtime vm options on the IntelliJ iDE, I tried to give some thing like 10k and it popped up this error stating it needs at least 64k for thread stack size.

Another question is that, how to find the default thread stack size of my jvm running in my embedded device from a java program?

Thanks,
Sen

Community
  • 1
  • 1
Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82
  • When your program has a lot of recursive calls, having a large stack size is helpful. Also stack is used to hold addresses so they can be retreived in constant time. – Vivin Aug 14 '14 at 14:23
  • I think [Adam Adamaszek's answer](http://stackoverflow.com/questions/4967885/jvm-option-xss-what-does-it-do-exactly) explains how this option is useful - if that is not the case for you, can you clarify what else you are looking for? – user2813274 Aug 14 '14 at 16:16
  • Please ask your second question in a separate post. It is quite a different topic. – apangin Aug 14 '14 at 22:53

1 Answers1

22

-Xss allows to configure Java thread stack size according to application needs:

  • larger stack size is for an application that uses recursive algorithms or otherwise deep method calls;
  • smaller stack size is for an application that runs thousands of threads - you may want to save memory occupied by thread stacks.

Bear in mind that HotSpot JVM also utilizes the same Java thread stack for the native methods and JVM runtime calls (e.g. class loading). This means Java thread stack is used not only for Java methods, but JVM should reserve some stack pages for its own operation as well.

The minimum required stack size is calculated by the formula:

(StackYellowPages + StackRedPages + StackShadowPages + 2*BytesPerWord + 1) * 4096

where

  • StackYellowPages and StackRedPages are required to detect and handle StackOverflowError;
  • StackShadowPages are reserved for native methods;
  • 2*4 (32-bit JVM) or 2*8 (64-bit JVM) is for VM runtime functions;
  • extra 1 is for JIT compiler recursion in main thread;
  • 4096 is the default page size.

E.g. for 32-bit Windows JVM minimum stack size = (3 + 1 + 4 + 2*4 + 1) * 4K = 68K

BTW, you may reduce the minumum required stack size using these JVM options: (not recommended!)

-XX:StackYellowPages=1 -XX:StackRedPages=1 -XX:StackShadowPages=1
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Hi apangin, I just want to know like where can I find articles and docs which can give more information on this. Searching for StackYellowPages in Google dint give me good results.. – Navaneeth Sen Aug 15 '14 at 10:10
  • @Sen I doubt there are individual articles on this, since it is the particular implementation and not a part of any specification. The best sources to leart about HotSpot are [the source code](http://hg.openjdk.java.net/jdk8u/hs-dev/hotspot/), [the mailing list](http://mail.openjdk.java.net/mailman/listinfo/hotspot-dev) and [the bugtracker](https://bugs.openjdk.java.net/browse/JDK/component/10304). – apangin Aug 15 '14 at 21:27