The concept and difference between heap and stack

Before talking about the heap and stack, let's talk about the division of JVM (virtual machine) memory:

A Java program needs to open up space when it runs, any software needs to open up space in memory when it runs, and a Java virtual machine also needs to open up space when it runs. The JVM opens up a memory area in the memory when it is running, and divides it more finely in its own memory area when it starts up. Because each piece of memory in the virtual machine is handled differently, it needs to be managed separately.

There are five divisions of JVM memory:

  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: The stack memory is first a memory area, which stores local variables. Anything defined in the method is a local variable (the one outside the method is a global variable), and the for loop defines a local variable, which is to load the function first. Local variables can only be defined, so the method goes to the stack first, and then defines the variable. The variable has its own scope. Once it leaves the scope, the variable will be released. The update speed of stack memory is very fast, because the life cycle of local variables is very short.

Heap memory: Arrays and objects are stored (in fact, arrays are objects). Everything created by new is in the heap. The heap stores all entities (objects), and entities are used to encapsulate data, and they encapsulate multiple (entities). multiple attributes), if a 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 all single variables, and the variables are released, then no more. 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 a legend:

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

The main function advances the stack, 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. In the heap, a space is first opened up through the new keyword. When the memory stores data, it is reflected by the address. The address is a continuous binary block, and then a memory address is allocated to the entity. Arrays all have an index. After the entity of the array is generated in the heap memory, each space will be initialized by default (this is the characteristic of the heap memory, uninitialized data cannot be used, but it can be used in the heap, Because it has been initialized, but not in the stack), the initialized values ​​of different types are different. So variables and entities are created in the heap and stack:

So how are heaps and stacks related?

We just said that an address is allocated to the heap, and the address of the heap is assigned to arr, and arr points to the array through the address. So when arr wants to manipulate the array, it passes the address instead of assigning all entities to it directly. This we no longer call him a basic data type, but a reference data type. Called arr refers to an entity in heap memory. (It can be understood as a pointer to c or c++. Java grows up from c++ and c++ is very similar, and optimizes c++)

if when int[] arr=null;

arr does not do any pointing, the role 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 a garbage and automatically reclaimed in an untimed time, because Java has an automatic reclamation mechanism, (but C++ does not, Programmers are required to recycle manually. If not, the heap will increase until the memory overflows, so Java is better than C++ in memory management). The automatic collection 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. The stack memory stores local variables and the heap memory stores entities;
  2. The update speed of stack memory is faster than heap memory, because the life cycle of local variables is very short;
  3. The variables stored in the stack memory will be released once the life cycle ends, and the entities stored in the heap memory will be reclaimed from time to time by the garbage collection mechanism.

Guess you like

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