Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is thread stack size option(-Xss) given to jvm? Why does it have a limit of atleast 68k in a windows pc?

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

like image 877
Navaneeth Sen Avatar asked Aug 14 '14 13:08

Navaneeth Sen


People also ask

What is thread stack size in Java?

Default Stack Size for JVM Internal Threads The default system stack size is 256 KB on all platforms. Note: The -Xss command line option sets the stack size of both application threads and JVM internal threads.

What is XSS in JVM?

Sets the maximum stack size for Java™ threads. To increase the maximum number of threads your system can support, reduce the maximum native stack size. -Xss <size> The default is 320 KB for 31-bit or 32-bit VMs and 1024 KB for 64-bit VMs.

What is XSS size?

Sets the maximum stack size for Java™ threads. -Xss <size> The default is 320 KB for 31-bit or 32-bit JVMs and 1024 KB for 64-bit JVMs. The maximum value varies according to platform and specific machine configuration.

How many stacks can a thread have in Java?

In Java SE 6, the default on Sparc is 512k in the 32-bit VM, and 1024k in the 64-bit VM. ... You can reduce your stack size by running with the -Xss option. ... 64k is the least amount of stack space allowed per thread.


1 Answers

-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
like image 162
apangin Avatar answered Sep 27 '22 23:09

apangin