Difference between heap and stack in Java

1. Overview

In Java, memory is divided into two types, one is stack memory and the other is heap memory.

2. Heap memory

1. What is heap memory?
Heap memory is a type of Java memory. Its function is to store objects and arrays in Java. When we create a new object or create an array, we will open up a space in the heap memory for it. for storage.

2. What are the characteristics of heap memory?
The first point: the heap can be regarded as a pipeline in a similar way, or the situation of queuing up to buy tickets is similar, so the characteristics of the heap memory are: first in, first out, last in and last out, that is, you queue first, okay, You buy tickets first.

The second point: the heap can dynamically allocate the memory size, and the lifetime does not have to be told to the compiler in advance, because it allocates memory dynamically at runtime, but the disadvantage is that the access speed is slower due to the dynamic allocation of memory at runtime. .

3. How is the new object allocated in the heap?
Managed by the automatic garbage collector of the Java virtual machine

3. Stack memory

1. What is stack memory
Stack memory is another kind of memory in Java, mainly used to execute programs, such as: basic type variables and object references Variable

2. Characteristics of stack memory The
first point: the stack memory is like a mineral water bottle. If you put something in it, the first one will sink to the bottom, so its characteristics are: first in, last out, last in first out

Second point : The access speed is faster than the heap, second only to the register, and the stack data can be shared, but the disadvantage is that the size and lifetime of the data stored in the stack must be determined, lacking flexibility

3. Stack memory allocation mechanism
Stack memory can be called For the first level cache, it is automatically reclaimed by the garbage collector

4. Data sharing
Example :
int a = 3;
int b = 3; The

first step of processing:
1. The compiler first processes int a = 3;
2. Creates a reference to variable a
3. Checks whether there is a value of 3 in the stack
4. If not found, will 3 is stored, a points to 3 The

second step of processing:
1. Process b=3
2. Create a reference to variable b
3. Find, directly assign

the third step change:
then
a = 4; the
same method as above
changes the value of a, a points to 4. The value of b is PS that will not change

: if it is two objects, it will be different. The object points to the same reference. If one changes, the other will also change

. 4. Stack and heap The difference

JVM is a stack-based virtual machine. The JVM allocates a stack for each newly created thread. That is to say, for a Java program, its operation is done by operating on the stack. The stack holds the state of the thread in frames. The JVM only performs two operations on the stack: push and pop operations in units of frames.

Difference
1. Heap memory is used to store objects and arrays created by new. 2. The stack memory is
used to store methods or local variables, etc.
3. The heap is first -in, first -
out, last-in, last-out to reclaim it, but for heap memory generally developers will reclaim it automatically



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326260754&siteId=291194637
Recommended