The difference between a heap and a stack in Java

  • 2020-04-01 03:40:46
  • OfStack

When a person starts to learn Java or other programming languages, they will come into contact with stacks and stacks. Because there is no clear explanation at the beginning, many people will have a lot of questions, what is a heap, what is a stack, and what is the difference between a heap and a stack? To make matters worse, there is a Last In First Out data structure called the Stack, which is java.util.Stack. In this case, it is inevitable that many people will be more difficult to understand the previous problem. In fact, both the heap and the stack are parts of memory that have different roles, and a program needs to allocate memory in that area. As we all know, all Java programs run inside the JVM virtual machine, and we're naturally talking about the heap and stack in the JVM's (virtual) memory.

The difference between

The difference between a heap and a stack in Java is, of course, a common interview question, and here are some of the specific differences

Do their job

The main difference is that stack memory is used to store local variables and method calls.
Heap memory is used to store objects in Java. Whether member variables, local variables, or class variables, the objects they point to are stored in heap memory.

Exclusive or Shared

Stack memory belongs to a single thread, each thread will have a stack memory, its stored variables can only be visible in its own thread, that is, the stack memory can be understood as the private memory of the thread.
Objects in heap memory are visible to all threads. Objects in heap memory can be accessed by all threads.

Exception error

If there is no available space stack memory storage method calls and local variables, the JVM throws Java. Lang. StackOverFlowError.
And if the heap memory is no space available to store the generated objects, the JVM throws Java lang. OutOfMemoryError.

The size

Stack memory is much less than heap memory, and if you use recursion, your stack will fill up quickly. If the recursion does not jump out in time, StackOverFlowError is likely to occur.
You can set the size of the stack by using the -xss option. The -xms option sets the starting size of the heap, and the -xmx option sets the maximum size of the heap.

This is the difference between a heap and a stack in Java. Understanding this can help you solve development problems, analyze heap and stack memory usage, and even tune performance.

View the default value (Updated)

Look at the default values for the heap, using the following code, where InitialHeapSize is the size of the original heap and MaxHeapSize is the maximum size of the heap.


13:17 $ java -XX:+PrintFlagsFinal -version | grep HeapSize
    uintx ErgoHeapSizeLimit                         = 0                                   {product}
    uintx HeapSizePerGCThread                       = 87241520                            {product}
    uintx InitialHeapSize                          := 134217728                           {product}
    uintx LargePageHeapSizeThreshold                = 134217728                           {product}
    uintx MaxHeapSize                              := 2147483648                          {product}
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

View the default value for the stack, where ThreadStackSize is the stack memory size.


13:21 $ java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
     intx CompilerThreadStackSize                   = 0                                   {pd product}
     intx ThreadStackSize                           = 1024                                {pd product}
     intx VMThreadStackSize                         = 1024                                {pd product}
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

The translation of information

The original English text: (link: http://javarevisited.blogspot.com.au/2013/01/difference-between-stack-and-heap-java.html).

On the basis of the original text, the translation has been revised, collated and deleted. If you are interested, you can visit the original text. The P.S. address has been walled.


Related articles: