Deep analysis of memory prototype and working principle in Java

This paper mainly explains its working principle in detail by analyzing the stack, heap and constant pool of JAVA memory allocation.

1、 Java virtual machine memory prototype

Register: we cannot control the stack in the program: store the basic type of data and object references, but the object itself is not stored in the stack, but in the heap. Heap: store the data generated by new. Static field: store the static member constant pool defined by static in the object: store constants. Non RAM storage: permanent storage space such as hard disk.

2、 Constant pool

Constant pools are defined at compile time and stored in compiled. Class file. In addition to the constant values (final) of various basic types (such as int, long, etc.) and object types (such as string and array) defined in the code, it also contains some symbolic references in the form of text, such as:

1. Fully qualified names of classes and interfaces; 2. The name and descriptor of the field; 3. Method and name and descriptor.

The virtual machine must maintain a constant pool for each mounted type. Constant pool is an ordered set and of constants used by this type, Include direct constants (string, integer and floating point constants) and symbolic references to other types, fields and methods. For string constants, its value is in the constant pool. While the constant pool in the JVM exists in memory in the form of a table. For string types, there is a constant_string_info table with a fixed length to store text string values. Note: this table only stores text String value, does not store symbol references. At this point, you should have a clear understanding of the storage location of string values in the constant pool. During program execution, the constant pool will be stored in the method area instead of the heap.

3、 Stack in JAVA memory allocation

The basic unit of stack is frame (or stack frame): whenever a java thread runs, the Java virtual opportunity allocates a Java stack to the thread. When the thread executes a Java method, it pushes a frame into the Java stack, which is used to store parameters, local variables, operands, intermediate operation results, etc. when the method is executed, the frame will pop up from the stack. All the numbers on the Java stack Data is private, and other threads cannot the stack data of this thread. Some basic types of variable data defined in the function and the reference variables of the object are allocated in the stack memory of the function. When a variable is defined in a code block, Java allocates memory space for the variable in the stack. When the variable exits the scope, Java will automatically release the memory space allocated for the variable, which can be used for other purposes immediately.

4、 Heap in JAVA memory allocation

The heap in the Java virtual machine is used to store objects and arrays created by new. The memory allocated in the heap is managed by the automatic garbage collection mechanism of the Java virtual machine. In short, compared with the stack, the heap is mainly used to store Java objects, and the stack is mainly used to store object references... After an array or object is generated in the heap, a special variable can also be defined in the stack, so that the value of the variable in the stack is equal to the first address of the array or object in the heap memory, and the variable in the stack becomes the reference variable of the array or object. The reference variable is equivalent to a name for the array or object. Later, the reference variable in the stack can be used in the program to access the array or object in the heap. A reference variable is equivalent to a name for an array or object.

Reference variables are ordinary variables, which are allocated in the stack when defined. Reference variables are released after the program runs outside its scope. The arrays and objects themselves are allocated in the heap. Even if the program runs outside the code block where the statement of using new to generate arrays or objects is located, the memory occupied by the arrays and objects themselves will not be released. The arrays and objects become garbage and cannot be used, but still occupy the memory space, It is collected (released) by the garbage collector at an uncertain time. This is why Java occupies more memory. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in java!

The Java heap is a runtime data area, Class (objects allocate space from them. These objects are established through new, newarray, anewarray, multianewarray and other instructions. They do not need program code to explicitly release. Heap is responsible for garbage collection. Heap has the advantage of dynamically allocating memory size and does not need to tell the compiler in advance, because it dynamically allocates memory at runtime, which is java garbage The collector will automatically collect the data that is no longer used. However, the disadvantage is that the access speed is slow due to the dynamic allocation of memory at run time.

The advantage of stack is that the access speed is faster than heap, second only to register, and stack data can be shared. However, the disadvantage is that the data size and lifetime in the stack must be determined and lack of flexibility. The stack mainly stores some basic types of variable data (int, short, long, byte, float, double, Boolean, char) and object handles (references).

Stack has a very important particularity, that is, the data in the stack can be shared. Suppose we also define:

int a=3; int b=3; The compiler processes int a = 3 first; First, it will create a reference with variable a in the stack, and then find out whether there is the value of 3 in the stack. If not, it will store 3, and then point a to 3 Then process int b = 3; After creating the reference variable of B, because there is already the value of 3 in the stack, point B directly to 3 In this way, both a and B point to 3 at the same time.

At this time, if you make a = 4; Then the compiler will re search whether there is a value of 4 in the stack. If not, it will store 4 and make a point to 4; If you already have, point a directly to this address. Therefore, the change of a value will not affect the value of B.

Note that this data sharing is different from the sharing of two object references pointing to one object at the same time, because in this case, the modification of a will not affect B. It is completed by the compiler, which is conducive to saving space. When an object reference variable modifies the internal state of the object, it will affect another object reference variable.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>