Java default stack size

I understand that each thread has its own stack . Primitive types and references are stored on the stack and that the object does not fit on the stack. My questions:

  • How much can the stack grow? (for example, with the parameters - Xms and - Xmx )
  • Can we limit its growth?
  • Does the stack have a minimum default value and a maximum value?
  • How does garbage collection work on the stack?
+49
java stack memory
Nov 17 '13 at 11:30
source share
2 answers

How much can the stack grow?

You can use the VM option called ss to configure the maximum stack size. The VM option is usually passed using -X {option}. So you can use java -Xss 1M to set the maximum stack size to 1M.

Each thread has at least one stack. Some Java Virtual Machines (JVMs) put the Java stack (calls to Java methods) and its own stack (calls to the Native method in the virtual machine) on the same stack and unwind the stack using the Managed to Native Frame, known as M2NFrame. Some JVMs save two stacks separately. Xss in most cases sets the size of the Java stack.

For many JVMs, they set different defaults for the stack size on different platforms.

Can we limit this growth?

When a method is called on the stack of this thread, a new stack stack will be created. The stack will contain local variables, parameters, return address, etc. In java, you can never push an object onto the stack, only a reference to the object can be stored on the stack. Since an array is also an object in java, arrays are also not stored on the stack. So, if you reduce the number of local primitive variables, parameters, grouping them into objects, you can reduce the space on the stack. In fact, the fact that we cannot push objects onto the java stack affects performance for a while (misses in the cache).

Does the stack value have a minimum default value or a maximum default value?

As I said, different virtual machines are different from each other and can change versions. See here .

How does garbage collection on the stack work?

Java garbage collections are a hot topic. Garbage collection aims to collect an unreachable object on the heap. So you need a definition of the achievable. Everything on the stack is part of the root links in the GC. Everything that is accessible from each pile of each thread should be considered as living. There are also some other root links, such as Thread objects and some class objects.

This is just a very vague use of the stack on the GC. Most JVMs currently use the GC generation. This article provides a brief description of the Java GC. And recently, I read a very good article about GC on .net. The GC on oracle jvm is pretty similar, so I think this can also help you.

+55
Nov 17 '13 at 13:07 on
source share

As you say, local variables and references are stored on the stack. When the method returns, the stack pointer simply moves back to where it was before the method started, that is, all local data is "deleted from the stack." Therefore, garbage collection is not required on the stack, which happens only on the heap.

To answer your specific questions:

  • See this question on how to increase stack size.
  • You can limit stack growth:
    • grouping many local variables in an object: this object will be stored on the heap, and only the link is stored on the stack
    • limit the number of nested function calls (usually without using recursion)
  • For windows, the default stack size is 320 thousand for 32-bit and 1024 thousand for 64-bit, see this link .
+8
Nov 17 '13 at 11:50
source share



All Articles