What the -Xss JVM option actually does

From the documentation, -Xss is used to set the size of the JVM stack. But I am very confused by this expression.

In Java, each thread has its own stack. The number specified in -Xss:

  • Shared memory that can be used as a stack for all threads? for example, if -Xss is set to 256K, all threads will create their own stack in this 256K memory.

  • The size of each stack of thread. for example, if -Xss is set to 256K, each thread will have a 256K stack. Therefore, 10 threads will use a total of 2560K.

Thank you very much.

EDIT:

Thank you for your responses. It looks like this is (2) senario above. -Xss indicates the largest stack size for a particular thread.

Then I have the following question: where will this memory be allocated?

We can specify the reserved heap memory using -Xmx and -Xms. Will a stack be allocated using this reserved memory? Or is it directly allocated from its own memory?

+5
source share
2 answers

This is the stack size per stream, quoting this page in the java command :

-Xss size

Sets the size of the stream stack (in bytes) ...

So this is the second part in your question. However, I do not think that in general it is possible to accurately summarize all the sizes of the stream stack. Depending on the JVM implementation, the actual total stack size may not be 2560K. Check out this quote from the JVM spec:

This specification allows the Java virtual machine stacks to either have a fixed size or dynamically expand and contract as required by the calculation. If the stacks of the Java virtual machine are of a fixed size, the size of each stack of the Java virtual machine can be selected independently when creating this stack.

+6
source

Each thread has its own stack. Most JVMs use their own threads, and these stacks use their own virtual memory. The advantage of using virtual memory is to use only the pages affected in memory.

Where will this memory be allocated to?

Own memory, similar to a stack stream in a C program.

We can specify the reserved heap memory using -Xmx and -Xms. Will a stack be allocated using this reserved memory?

Stacks don't use a bunch, so no.

Or is it allocated from its own memory directly?

Yes.

+3
source

All Articles