JVM
-Xss Option
Java Virtual Machine
Programming
Java Options

JVM -Xss option: what does it do exactly?

Codemia Knowledge Hub ·

-Xss sets the stack size of each thread the JVM creates, for example java -Xss512k MyClass. The stack size limits how deep method calls can nest: too small and deep recursion throws StackOverflowError, too large and many threads waste memory.

What the thread stack holds

Every Java thread has its own stack, the memory region where it stores method call frames and local variables. Each method call pushes a new frame onto the stack of the thread running it, so the stack size decides how deep the chain of calls can go. That matters for recursive algorithms and heavily nested calls.

If the stack is too small, a call chain that needs more space than configured causes a stack overflow, typically in deep recursion. If it is very large, memory is wasted, and with a large number of threads the system can become unstable.

When the setting matters

-Xss defines the maximum stack each thread can use:

  • Recursive algorithms: some algorithms need a deep call stack. A deeply recursive Fibonacci implementation, for instance, needs a large stack for its deeper recursion levels.
  • Many threads: in applications that spawn many threads, weigh memory use per thread against the risk of stack overflow.
  • System stability: a stack size set too high on a system running many threads can exhaust memory.

Setting -Xss

Pass it when starting the application. This sets a stack of 512 KB per thread:

bash
java -Xss512k MyClass

When tuning, it is common to try several sizes and measure the effect on performance and stability.

Example: recursion that overflows the default stack

java
1public class Factorial {
2    public static long factorial(int n) {
3        if (n == 1) return 1;
4        else return n * factorial(n-1);
5    }
6
7    public static void main(String[] args) {
8        System.out.println(factorial(100000));
9    }
10}

With the default stack size, this throws StackOverflowError, because computing the factorial of 100000 needs 100000 nested calls. Raising -Xss (for example -Xss64m) lets it run without error. The long result itself overflows, so the example only demonstrates stack depth.

Defaults and trade-offs

FactorDescription
Default ValueTypically 512K or 1M, depending on the platform.
Use CasesEssential for applications using deep recursion or a large number of threads.
RisksSetting it too low can cause StackOverflowError; too high might waste memory or affect stability.
Typical SettingDepends on application needs; requires testing and profiling for optimization.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms