Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a java thread's stack size at runtime

Does anyone know if there is a way to dynamically (runtime) increase the stack size of the main Thread? Also, and I believe it is the same question, is it possible to increase / update the stack size of a Thread after its instantiation?

Thread’s CTOR allows the definition of its stack size but I can’t find any way to update it. Actually, I didn’t find any management of the stack size in the JDK (which tends to indicate that it’s not possible), everything is done in the VM.

According to the java language specification it is possible to set the stack size ‘when stack is created’ but there is a note:

A Java virtual machine implementation may provide the programmer or the user control over the initial size of Java virtual machine stacks, as well as, in the case of dynamically expanding or contracting Java virtual machine stacks, control over the maximum and minimum sizes.

IMO that’s not very clear, does that mean that some VM handle Threads with max (edit) stack sizes evolving within a given range? Can we do that with Hostpot (I didn't find any stack size related options beside Xss) ?

Thanks !

like image 944
Jerome Avatar asked Apr 13 '12 06:04

Jerome


People also ask

How do you change the size of a stack in Java?

The setSize() method of Java. util. Stack class changes the size of this Stack instance to the size passed as the parameter. Parameters: This method takes the new size as a parameter.

Does stack size change at runtime?

The amount of stack used certainly increases, as you allocate local variables and make function calls. Whether the stack's maximum size can grow is technically undefined, but in practice is generally constant.

Can stack size be changed?

You can change the stack size after the .exe file is built by using the EDITBIN tool.

How do you increase Stacksize?

To increase the stack size, define the new value in the -Xss setting in the JAVA_OPTS entry in your Tomcat start script file (/opt/netiq/idm/apps/tomcat/bin/setenv.sh or C:\NetIQ\idm\apps\tomcat\bin\setenv. bat). For example, to set the stack size to 4M, change the setting to -Xss4M.


1 Answers

The stack size dynamcally updates itself as it is used so you never need to so this.

What you can set is the maximum size it can be with -Xss This is the virtual memory size used and you can make it as large as you like on 64-bit JVMs. The actual memory used is based on the amount of memory you use. ;)

EDIT: The important distinction is that the maximum size is reserved as virtual memory (so is the heap btw). i.e. the address space is reserved, which is also why it cannot be extended. In 32-bit systems you have limited address space and this can still be a problem. But in 64-bit systems, you usually have up to 256 TB of virtual memory (a processor limitation) so virtual memory is cheap. The actual memory is allocated in pages (typically 4 KB) and they are only allocated when used. This is why the memory of a Java application appears to grow over time even though the maximum heap size is allocated on startup. The same thing happens with thread stacks. Only the pages actually touched are allocated.

like image 173
Peter Lawrey Avatar answered Sep 29 '22 13:09

Peter Lawrey