Java.Multithreading.What is the JVM startup parameter used to control the size of a thread stack?

🔍 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:

ValueMeaning
-Xss256k256 kilobytes
-Xss1m1 megabyte
-Xss2m2 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.

This entry was posted in Без рубрики. Bookmark the permalink.