admin管理员组

文章数量:1357273

How does the -Xss play a key role in java program?

The size of the stack directly affects the program running.

  1. As I know -Xss decides the thread stack size of jvm, includ local variables, operand stacks, dynamic link information, return addresses and so on.
  2. Usually in my local development environment, I usually set -Xss 4M.

I ran a simple recursive program, and it StackOverflowError.

public class StackOverflowErrorExample {

    private static int count = 0;

    public static void recursiveFunction() {
        count++;
        recursiveFunction(); // recursive
    }

    public static void main(String[] args) {
        try {
            recursiveFunction(); 
        } catch (StackOverflowError e) {
            System.out.println("overflow: " + count);
            e.printStackTrace();
        }
    }
}

How does the -Xss play a key role in java program?

The size of the stack directly affects the program running.

  1. As I know -Xss decides the thread stack size of jvm, includ local variables, operand stacks, dynamic link information, return addresses and so on.
  2. Usually in my local development environment, I usually set -Xss 4M.

I ran a simple recursive program, and it StackOverflowError.

public class StackOverflowErrorExample {

    private static int count = 0;

    public static void recursiveFunction() {
        count++;
        recursiveFunction(); // recursive
    }

    public static void main(String[] args) {
        try {
            recursiveFunction(); 
        } catch (StackOverflowError e) {
            System.out.println("overflow: " + count);
            e.printStackTrace();
        }
    }
}
Share Improve this question edited Mar 28 at 6:12 MoXi asked Mar 28 at 3:11 MoXiMoXi 12 bronze badges 2
  • -Xss4M overflow count : 216555 in my computer. – MoXi Commented Mar 28 at 3:25
  • 2 In general, the most appropriate setting is no setting. Use the default. – Stephen C Commented Mar 28 at 3:58
Add a comment  | 

1 Answer 1

Reset to default 1

What is the appropriate value of the jvm -Xss parameter?

It depends. But typically, you wouldn't set it at all.

How does the -Xss play a key role in java program?

The role is that is sets the default stack size for threads. Whether it is a "key" role depends on the application. However:

  • if the stack size is too small, some (otherwise correct1) programs will fail
  • if the stack size is too large you will waste memory, especially if you have many threads.

Note that there will be stack for each application thread, and unless the thread is created with an explicit stack size (i.e. using this constructor) the -Xss value (or its default) will be used for each thread's stack.

The size of the stack directly affects the program running.

In some cases. In most cases the default stack size (i.e. what you get if you don't specify -Xss) is just fine. It depends on the application.

Finally, your test code is going overflow the stack no matter what -Xss is set to. It is simply a pathological program ... and it is not useful for deciding what -Xss a typical real application requires.


1 - Correctness is nuanced. For example, given that we know that Java implementations don't implement tail-call elimination, it is debatable whether it is correct to implement traversal of a linked list using recursion. If you do, and the list is large, and the stack isn't large enough, then the application will SOE.

本文标签: javaWhat is the appropriate value of the jvm Xss parameterStack Overflow