JVM option -Xss - What does it do exactly?

285.6k Views Asked by At

It says here that -Xss is used to "set thread stack size", what does it mean exactly? Could anyone help me understand this?

5

There are 5 best solutions below

8
On BEST ANSWER

Each thread in a Java application has its own stack. The stack is used to hold return addresses, function/method call arguments, etc. So if a thread tends to process large structures via recursive algorithms, it may need a large stack for all those return addresses and such. With the Sun JVM, you can set that size via that parameter.

0
On

Each thread has a stack which used for local variables and internal values. The stack size limits how deep your calls can be. Generally this is not something you need to change.

0
On

If I am not mistaken, this is what tells the JVM how much successive calls it will accept before issuing a StackOverflowError. Not something you wish to change generally.

2
On

It indeed sets the stack size on a JVM.

You should touch it in either of these two situations:

  • StackOverflowError (the stack size is greater than the limit), increase the value
  • OutOfMemoryError: unable to create new native thread (too many threads, each thread has a large stack), decrease it.

The latter usually comes when your Xss is set too large - then you need to balance it (testing!)

0
On

Add my two cents here, besides what mentioned, we can write a simple demo to show the effect of setting Xss.

Generally speaking, it controls the stack size arranged to each thread.

    public static void main(String[] args) {
        try{
            recur();
        }catch (StackOverflowError e){
            System.out.println(depth);
        }
    }

    static int depth = 1;

    public static void recur(){
        depth++;
        recur();
    }

After compiling above code, you will see the depth (the invoke hierachy) grows together with the passed Xss settings.

The output of java -Xss1m com.eugene.Main is 21638 and the output of java -Xss2m com.eugene.Main is 48325 at my local machine.