🔍 In the Java Virtual Machine (JVM), you can control the size of each thread’s stack using the following startup parameter:
✅ -Xss<size>
This option sets the stack size per thread.
💡 Example:
java -Xss512k MyApp
This sets each thread’s stack size to 512 KB.
📐 Common Sizes:
Value | Meaning |
---|---|
-Xss256k | 256 kilobytes |
-Xss1m | 1 megabyte |
-Xss2m | 2 megabytes |
🧠 Why It Matters:
- Too small: You’ll get a
StackOverflowError
in deep recursions or heavily nested calls. - Too large: You may run out of memory quickly, especially with many threads.
📌 Default Size:
The default depends on:
- OS
- Architecture (32-bit vs 64-bit)
- JVM implementation
Typical defaults:
- ~1 MB on most 64-bit JVMs
- ~512 KB on 32-bit JVMs
You can check it using tools like jstack
or estimate by spawning threads until OOM.