Error java.lang.StackOverflowError occur if I don't explicitly set -XX:ThreadStackSize. As I do some research, the default value of ThreadStackSize is 1024K without explicitly set it. The confusing part is if I explicitly set it to 124K, 256K or 1024K, It will solves the problem.
My environment info:
I have a micro-service using Java, Spring Framework, Docker, Kubernetes. My server environment is using Linux 64bit.
Here is my Java info:
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
intx CompilerThreadStackSize = 1024 {pd product} {default}
intx ThreadStackSize = 1024 {pd product} {default}
intx VMThreadStackSize = 1024 {pd product} {default}
openjdk version "17.0.8.1" 2023-08-24
OpenJDK Runtime Environment Temurin-17.0.8.1+1 (build 17.0.8.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.8.1+1 (build 17.0.8.1+1, mixed mode, sharing)
In my source code, I have a deep recursive method which cause this error.
I want to know why this case happen and any official reference documents will be helpful. I want to know the reference to explain it to others.
Error java.lang.StackOverflowError occur if I don't explicitly set -XX:ThreadStackSize. As I do some research, the default value of ThreadStackSize is 1024K without explicitly set it. The confusing part is if I explicitly set it to 124K, 256K or 1024K, It will solves the problem.
My environment info:
I have a micro-service using Java, Spring Framework, Docker, Kubernetes. My server environment is using Linux 64bit.
Here is my Java info:
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
intx CompilerThreadStackSize = 1024 {pd product} {default}
intx ThreadStackSize = 1024 {pd product} {default}
intx VMThreadStackSize = 1024 {pd product} {default}
openjdk version "17.0.8.1" 2023-08-24
OpenJDK Runtime Environment Temurin-17.0.8.1+1 (build 17.0.8.1+1)
OpenJDK 64-Bit Server VM Temurin-17.0.8.1+1 (build 17.0.8.1+1, mixed mode, sharing)
In my source code, I have a deep recursive method which cause this error.
I want to know why this case happen and any official reference documents will be helpful. I want to know the reference to explain it to others.
Share Improve this question edited Feb 7 at 8:58 Mark Rotteveel 109k226 gold badges155 silver badges219 bronze badges asked Feb 7 at 8:02 Lonely DeveloperLonely Developer 12 bronze badges New contributor Lonely Developer is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1- 1 This question is similar to: What is a StackOverflowError?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Stultuske Commented Feb 7 at 8:34
1 Answer
Reset to default 1Your confusion stems from the fact that the default value is 1024
, not 1024K
, which would be reported as 1048576
in the flags print out.
The value of the ThreadStackSize
setting is in kilobytes. So, the default of 1024
means a stack size of 1 MiB, while setting it to 1024K
results in a stack size of 1 GiB (1024 times larger than the default).
In other words, your deep recursive method requires more than 1 MiB of stack, while it succeeds with 124 MiB of stack (the 124K
setting) or higher (the 256K
or 256 MiB, and 1024K
or 1 GiB values you tried). You may want to consider not using a recursive method, but instead using an iterative approach.
As an aside, you can also use the -Xss
command line option to set the stack size. This option does expect a value in bytes (rounding up to whole kilobytes). The option -XX:ThreadStackSize=1024
is equivalent to -Xss1024k
.