JVM option -Xss - What does it do exactly?
Asked Answered
C

5

281

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

Chemisette answered 11/2, 2011 at 10:22 Comment(1)
Some examples here (not mine ...): herongyang.com/JVM/…Trimurti
S
307

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.

Soulful answered 11/2, 2011 at 10:25 Comment(8)
So, The -Xss option is used to limit how much memory a stack consumes (by storing return addresses, variables etc) and which also indirectly limits how deep a stack can get? Am I correct?Chemisette
@instantsetsuna: I think the more common use is probably to increase the default limit. (There's always a limit.) But yes, you're controlling the size of the stack, which controls how deep the stack can get.Soulful
how do you do the equivalent of this XSS setting on the java compiler (aka javac)? Its an issue for those who use scala-based libraries that cause large tail recursion to happen in the compilation of the classes – Andrew Norman 9 secs agoUltimately
@AndrewNorman: You don't compile Java runtime options into the class file, that's more an environment-specific thing. If you really need to do it in code, you can write a tiny main class whose sole job is to launch your real application with the options you need.Soulful
@AndrewNorman You can give JVM configuration flags the compiler should run with using -Jflag syntax (eg. -J-Xss).Songstress
@Songstress unfortunately the -J-Xss or -JXss does not work with SBT scripts. At least not with class compilingUltimately
@T.J.Crowder my issue is with SBT scripts not with compiled classes. Its the compiling of the classes that is the issue (not the running of the classes). Trying to find an inline setting that I can store in a repo rather than manually setting the XSS on every single build and dev machine. If you use Scala with Slick / shapeless persistence and big tables you'll hit this issue when you've described a table with 80-90 columns. This is because this library describes the tables with a compile-time tail recursion per defined column.Ultimately
I talk more about my scenario in this thread. See my "workaround solution" -> #19826309Ultimately
J
202

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!)

Justify answered 28/9, 2012 at 16:35 Comment(2)
Not necessarily every time actually. Both SOE and OOME could occur because of different reasons that should be fixed differently.Hybris
True, but I didn't say -Xss is the only cause for SOE and OOME, but the other way round - if set incorrectly, it can cause one of the two.Justify
R
9

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.

Romanism answered 11/2, 2011 at 10:24 Comment(0)
V
5

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.

Voracious answered 11/2, 2011 at 10:27 Comment(0)
I
4

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.

Indoctrinate answered 8/8, 2022 at 5:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.