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:
When tuning, it is common to try several sizes and measure the effect on performance and stability.
Example: recursion that overflows the default stack
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
| Factor | Description |
| Default Value | Typically 512K or 1M, depending on the platform. |
| Use Cases | Essential for applications using deep recursion or a large number of threads. |
| Risks | Setting it too low can cause StackOverflowError; too high might waste memory or affect stability. |
| Typical Setting | Depends on application needs; requires testing and profiling for optimization. |
Related reading
- JVM signal chaining SIGPIPE
- JWT decoding with Spring Security
- jython multithreading
- Kafka - Could not find a 'KafkaClient' entry in the JAAS configuration java
- Kafka - Unable to send a message to a remote server using Java
- Kafka - What are the better alternatives than poll() to listen to a topic in Java?
- Kafka 0.10 Java Client TimeoutException Batch containing 1 record(s) expired
- Kafka 0.8, is it possible to create topic with partition and replication using java code?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.