Home  >  Article  >  Java  >  The difference between heap and stack java

The difference between heap and stack java

angryTom
angryTomOriginal
2019-11-13 14:32:204384browse

The difference between heap and stack java

The difference between heap and stack java

Before talking about heap and stack, let’s talk about JVM (virtual Machine) memory division:

Java programs must allocate space when they are running. Any software must allocate space in the memory when they are running. The Java virtual machine also must allocate space when it is running. The JVM opens up a memory area in the memory when it is running, and divides it more carefully in its own memory area when it starts. Because each piece of memory in the virtual machine is processed in a different way, it must be managed separately.

The JVM memory is divided into five parts:

1. Register;

2. Local method area;

3. Method area;

4. Stack memory;

5. Heap memory.

Let’s focus on the heap and stack:

Stack memory: Stack memory is first of all a memory area, which stores local variables, all defined in methods All are local variables (global variables outside the method). Local variables are also defined inside the for loop. The function must be loaded first before local variables can be defined. Therefore, the method first stacks the stack and then defines the variables. The variables have their own scope. , the variable will be released once it leaves the scope. The stack memory is updated very quickly because the life cycle of local variables is very short.

Heap memory: stores arrays and objects (in fact, arrays are objects). Everything created by new is in the heap. Entities (objects) are stored in the heap. Entities are used to encapsulate data, and It encapsulates multiple (multiple attributes of an entity). If one piece of data disappears, the entity does not disappear and can still be used, so the heap will not be released at any time, but the stack is different. The stack stores only a single variable. Once the variable is released, it is gone. Although the entities in the heap will not be released, they will be treated as garbage. Java has a garbage collection mechanism to collect them from time to time.

Let’s talk about the heap and stack in detail through an illustration:

For example, the statement in the main function int [] arr=new int [3]; in memory How it is defined:

The main function goes to the stack first, defines a variable arr in the stack, and then assigns a value to arr, but the right side is not a specific value, but an entity. The entity is created in the heap. First, a space is opened in the heap through the new keyword. When the memory stores data, it is reflected by the address. The address is a continuous binary, and then a memory address is allocated to the entity. Arrays have an index. After the array entity is generated in the heap memory, each space will be initialized by default (this is a characteristic of heap memory. Uninitialized data cannot be used, but it can be used in the heap. Because it has been initialized but not on the stack), different types have different initialized values. So variables and entities are created in the heap and stack:

The difference between heap and stack java

So how are the heap and stack connected?

We just said that we allocate an address to the heap, assign the address of the heap to arr, and arr points to the array through the address. So when arr wants to manipulate the array, it uses the address instead of assigning entities to it directly. We no longer call this a basic data type, but a reference data type. It is called arr and refers to the entity in the heap memory. (Can be understood as a pointer to c or c. Java grew up from c and is very similar to c, optimizing c)

The difference between heap and stack java

If when int [] arr=null;

arr does not point to anything, and the function of null is to dereference the pointer of the data type.

When an entity is not pointed to by a reference data type, it will not be released in the heap memory, but will be treated as garbage and automatically recycled at an irregular time, because Java has an automatic recycling mechanism. , (while c does not, the programmer needs to manually recycle. If it is not recycled, the heap will grow until it is full and the memory overflows, so Java is better than c in memory management). The automatic recycling mechanism (program) automatically monitors whether there is garbage in the heap. If there is garbage, it will automatically perform garbage collection, but it is not certain when it will be collected.

So the difference between heap and stack is obvious:

1. Stack memory stores local variables while heap memory stores entities;

2. The update speed of stack memory is faster than that of heap memory, because the life cycle of local variables is very short;

3. The variables stored in stack memory will be released once their life cycle ends, while the entities stored in heap memory will be released. It will be collected by the garbage collection mechanism from time to time.

php Chinese website, a large number of free Java introductory tutorials, welcome to learn online!

The above is the detailed content of The difference between heap and stack java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn