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

前端 未结 1 1372
生来不讨喜
生来不讨喜 2021-02-06 13:29

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 va

相关标签:
1条回答
  • 2021-02-06 14:19

    -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
    
    0 讨论(0)
提交回复
热议问题