Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can one obtain actual stack size used by a thread in Java after some time of running?

The idea would be to help determine the optimum stack size for a given Java application.

One thing that could be done with this information create a range-table of stack sizes which the threads could modify as they exit and which could be dumped periodically and at application exit.

EDIT: This is in the context of running on customer machines with real workloads which I can't get profiler access to.

EDIT2: In response to one answer, at (IIRC) 256Kb per thread, I have wondered for a while now how close that is to the reality of what's needed (I also wonder if this question might be not very relevant because perhaps stack space is allocated on demand). We have an application server which is based on message passing and highly threaded and runs on everything from an ARM handheld to octo-core Linux, to midrange and mainframes - it would be good to have a feeling for where (and if) we can trade stack space for heap on systems with many message handlers.

There are some similar questions which are of interest, but they are native/os-specific:

  • How to determine maximum stack usage?
  • How to determine optimal thread stack size?
like image 945
Lawrence Dol Avatar asked May 05 '09 18:05

Lawrence Dol


People also ask

Is stack size fixed in Java?

If we don't specify a size for the stacks, the JVM will create one with a default size. Usually, this default size depends on the operating system and computer architecture. For instance, these are some of the default sizes as of Java 14: Linux/x86 (64-bit): 1 MB.

Can we change stack size?

You may need to increase the stack size if your program gets stack-overflow messages at runtime. You can also set the stack size by: Using the /STACK linker option. For more information, see /STACK (Stack allocations).

What is the default Java thread stack size?

Sets the maximum stack size for Java™ threads. The default is 320 KB for 31-bit or 32-bit JVMs and 1024 KB for 64-bit JVMs. The maximum value varies according to platform and specific machine configuration.

Do threads have their own stack Java?

Within Java, the Java threads are represented by thread objects. Each thread also has a stack, used for storing runtime data. The thread stack has a specific size. If a thread tries to store more items on the stack than the stack size allows, the thread will throw a stack overflow error.


1 Answers

Stack memory will be hard to obtain.

Best you can do, and quite easily, is JVM memory usage via MemoryMXBean.

like image 103
Yuval Adam Avatar answered Oct 16 '22 15:10

Yuval Adam