How do I set Java thread stack size?
Environment
- Java
Issue
- How to increase the stack size in order to avoid a
java.lang.StackOverflowError. - How to decrease stack size to save memory.
- How need to change the JVM stack size.
- How to set Java thread stack size on Windows?
Resolution
All JDKs except Sun JDK on Solaris:
Use -Xss Java VM option and increase/decrease the value by increments of 64k. Note this accepts unites of k, m or g, so 128k results in a stack of 128 kb. For example:
-Xss128k
Sun JDK on Solaris:
Use -XX:ThreadStackSize Java VM option and increase/decrease the value by increments of 64k. Content from docs.oracle.com is not included.Per java documentation, note that -XX:ThreadStackSize sets the Java thread stack size in kilobytes. Any suffix, such as k, results in the scaling of the kilobytes value so -XX:ThreadStackSize=128k sets the java thread stack size to 128kb * 1024 or 128 mb instead. So for example, a thread stack of 128 kb would instead be set as below with this option:
-XX:ThreadStackSize=128
Linux:
These is a ulimit setting that defines the upper limit of a stack size at the OS level. Run the following to see the upper limit in kilobytes:
ulimit -s
If decreasing the thread stack size results in java.lang.StackOverflowError, increase by increments of 64k until the StackOverflowError goes away.
Refer What is the default Java thread stack size? for more information.
Diagnostic Steps
-
You can check the current thread stack size by the following command:
jinfo -flag ThreadStackSize <JAVA_PID>
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.