Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -Xss JVM option actually do

Tags:

java

jvm

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

In Java, every thread has its own stack. Does the number specified by -Xss:

  1. The total memory that can be used as stack by all threads? e.g. If -Xss is set to 256K, all the threads will create their own stack within this 256K of memory.

  2. The size of each stack of a thread. e.g. If -Xss is set to 256K, each thread will have a stack of 256K big. Hence 10 thread will use 2560K altogether.

Thank you very much.

EDIT:

Thanks for your answers. It looks like it is the (2) senario above. -Xss specifies the largest stack size of a particular thread.

Then I have a follow up question: Where will these memory be allocated on?

We can specify the reserved heap memory using -Xmx and -Xms. Will stack be allocated using these reserved memory? Or it is allocated from the native memory directly?

like image 809
Kevin Avatar asked Jul 07 '16 10:07

Kevin


2 Answers

Each thread has its own stack. Most JVMs use native threads and these stacks use native virtual memory. The advantage of using virtual memory is only the pages touched turn into memory used.

Where will these memory be allocated on?

Native memory like threads stack in a C program would.

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

The stacks don't use the heap so, no.

Or it is allocated from the native memory directly?

yes.

like image 123
Peter Lawrey Avatar answered Sep 27 '22 23:09

Peter Lawrey


It's the size of the stack per thread, quoting this page on the java command:

-Xsssize

Sets the thread stack size (in bytes)...

So it's the second part in your question. However I don't think it is generally accurate to sum up all thread stack sizes. Depending on the JVM implementation, the actual total stack size may not be 2560K. Note this quote from the JVM spec:

This specification permits Java Virtual Machine stacks either to be of a fixed size or to dynamically expand and contract as required by the computation. If the Java Virtual Machine stacks are of a fixed size, the size of each Java Virtual Machine stack may be chosen independently when that stack is created.

like image 37
M A Avatar answered Sep 27 '22 23:09

M A