5

I want to change the default thread stack size in Java for my Spring-Boot Web application. How can I change this? or which parameter I need to use to change this value?

Also, the second question is, can I programmatically change this at runtime?

Ashutosh Chamoli
  • 987
  • 1
  • 11
  • 27
ZestBest
  • 55
  • 6
  • May be you can look into this, which is a similar post https://stackoverflow.com/questions/407612/how-to-get-a-thread-and-heap-dump-of-a-java-process-on-windows-thats-not-runnin – Ashutosh Chamoli Oct 28 '20 at 16:03
  • 1
    Hi @AshutoshChamoli, thanks for the help, let me look into this, it may help. – ZestBest Oct 28 '20 at 16:03
  • the real question is why you would want to do that, stacks are filled lazy – Eugene Oct 28 '20 at 18:06
  • Hey @Eugene, what do you mean by `stacks are filled lazy`, can you please explain or share any documentation. Also, in case, I want threads of smaller stack size, just to increase the number of parallel threads my server can handle, what shall I do? – ZestBest Oct 29 '20 at 05:15
  • 1
    @ZestBest not exactly documentation, but this article of mine contains some experiments illustrating lazy stack filling with Docker memory limits - https://bell-sw.com/announcements/2020/10/28/JVM-in-Linux-containers-surviving-the-isolation/ – Alexey Ragozin Oct 29 '20 at 19:11
  • @AlexeyRagozin you are part of bellsoft too?? That is one dream team right there! – Eugene Oct 30 '20 at 03:40

2 Answers2

2

Yes, you can do that. Basically, you can configure the max Stack size of your thread in your application.

For this, you can use the option named ss to adjust the maximum stack size. A VM option is usually passed using -X{option}. So you can use java -Xss1M to set the maximum of stack size to 1M.

One more example is java -Xss1048576, this sets the max thread size to approximately 1 MB

Checkout the two below blogs also for more info and other flags.

-> https://www.baeldung.com/jvm-configure-stack-sizes

-> https://docs.gigaspaces.com/latest/production/production-jvm-tuning.html

ZestBest
  • 55
  • 6
Ashutosh Chamoli
  • 987
  • 1
  • 11
  • 27
2

I want to add a couple of very useful resources:

The default is 1M per thread, but fortunately the things are not so bad. OS allocates memory pages lazily, i.e. on the first use, so the actual memory usage will be much lower (typically 80-200 KB per thread stack).

in JDK 11/12 [minimum thread stack size] is: min_stack_size = 40 KB + (1 + 2 + 1 + 20) * 4 KB = 136 KB

If I try to set a lower value, I’ll expectedly get an error:

/usr/java/jdk-12.0.2/bin/java -Xss100k

The Java thread stack size specified is too small. Specify at least 136k

Juraj Martinka
  • 3,991
  • 2
  • 23
  • 25
  • 1
    @ZestBest I think the first one (https://stackoverflow.com/questions/53451103/java-using-much-more-memory-than-heap-size-or-size-correctly-docker-memory-limi/53624438#53624438) also answers your question regarding "stacks are filled lazy," – Juraj Martinka Oct 29 '20 at 10:55