This article mainly through the analysis of Java memory allocation stack, heap and Chang detailed explanation of its working principle.
First, Java Virtual machine memory prototype
Registers: We can not control the stack in the program: hold the basic type of data and object references, but the object itself is not stored in the stack, Instead, it is stored in a heap: a static field of data that is generated with new: A static member Chang in an object that is statically defined: holds constant, non-RAM storage: Persistent storage space such as a hard disk.
Ii. Chang (Constant pool)
Chang refers to being determined at compile time and is saved in the compiled. Some of the data in the class file. The constant value (final) that contains the various base types defined in the code (such as int, long, and so on) and the object type (such as String and array) also contains some symbolic references that appear as text, such as:
1. Fully qualified name of class and interface;
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. A constant pool is an ordered set of constants used by the type, including direct constants (String,integer and floating point constants) and symbolic references to other types, fields, and methods. For a string constant, its value is in a constant pool. The Chang in the JVM exists as a table in memory, and for string types, a fixed-length constant_string_info table is used to store literal string values, noting that the table stores only literal string values and does not store symbol references. Here, there should be a clearer understanding of where to store string values in a constant pool. When the program executes, the constant pool is stored in method area, not in the heap.
Stacks in Java memory allocation
The base unit of a stack is a frame (or stack frame): Whenever a Java thread is running, the Java virtual Opportunity assigns a Java stack to that thread. When the thread executes a Java method, it presses a frame into the Java stack, which is used to store parameters, local variables, operands, the results of intermediate operations, and so on. When this method is done, the frame pops out of the stack. All the data on the Java stack is private, and no other thread can have the stack data for that thread. Some basic types of variable data and object reference variables defined in a function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates memory space for the variable in the stack, and when the variable exits the scope, Java automatically releases the memory space allocated for the variable, which can be used as an immediate alternative.
Heap in Java memory allocation
The heap in the Java Virtual machine is used to hold objects and arrays created by new. Memory allocated in the heap, the automatic garbage collection mechanism of the Java Virtual machine to manage the memory of the heap. Simple say and stack relative, the heap is mainly used to store Java objects, the stack is mainly used to hold the object reference ... After generating an array or object in the heap, you can also define a special variable in the stack so that the value of the variable in the stack equals the first address of the array or object in the heap memory, and the variable in the stack becomes the reference variable for the array or object. A reference variable is equivalent to a name that is an array or an object, and you can later use the reference variable in the stack to access the array or object in the heap. A reference variable is equivalent to a name that is an array or an object.
A reference variable is a normal variable that is assigned in the stack when defined, and the reference variable is released after the program runs beyond its scope. and the arrays and the objects themselves are allocated in the heap, even if the program runs to a block of code that uses new to produce an array or an object's statements, the memory occupied by the array and the object itself is not freed, and the array and object become garbage when they point to it without a reference variable, and cannot be used, but still occupy the memory space. Be taken Away (released) by the garbage collector at a later uncertain time. This is why Java comparisons account for memory. In fact, the variables in the stack point to the variables in the heap memory, which is the pointer in Java!
Java's heap is a run-time data area in which the object allocates space. These objects are established through directives such as new, NewArray, Anewarray, and Multianewarray, and they do not require program code to be explicitly released. The heap is responsible for garbage collection, the advantage of the heap is the dynamic allocation of memory size, the lifetime does not have to tell the compiler in advance, because it is dynamically allocating memory at runtime, the Java garbage collector will automatically take away these no longer used data. The disadvantage is that the access rate is slow due to the dynamic allocation of memory at run time.
The advantage of the stack is that the access speed is faster than the heap, after the register, the stack data can be shared. The disadvantage is that the data size and lifetime in the stack must be fixed and inflexible. There are some basic types of variable data in the stack (int, short, long, byte, float, double, Boolean, char) and object handles (references).
Stack has a very important particularity, is the existence of the stack of data can be shared. Let's say we both define:
int a=3; int b=3; The compiler handles int a = 3 first; It first creates a reference to a variable in the stack, then find out if the stack has a value of 3, if not found, put 3 in, and then point A to 3. then process int b = 3; After you create the reference variable for B, because there is already a value of 3 in the stack, Then point B directly to 3. In this way, A and B point to 3 at the same time.
At this point, if you make a=4 again, the compiler will search for a 4 value in the stack, and if not, put 4 in and point A to 4, and if so, direct a to this address. Therefore the change of a value does not affect the value of B.
Note that this sharing of data is different from the two-object reference to an object, because the modification of a does not affect B, it is done by the compiler, and it helps save space. While an object reference variable modifies the internal state of the object, it affects another object reference variable.