Memory prototype of Java Virtual Machine
Register:InProgramCannot be controlled
STACK:Stores basic types of data and object references, but the object itself is not stored in the stack, but stored in the heap
Heap:Store data generated with new
Static domain:Static members stored in objects defined by static
Constant pool:Store Constants
Non-ram storage:Permanent storage space such as hard disk
Stack in Java Memory Allocation
The basic unit of a stack is frame (or stack frame). When a Java thread runs, the Java VM allocates a java stack to the thread. When executing a Java method, this thread pushes a frame to the java stack, which is used to store parameters, local variables, operands, intermediate calculation results, and so on. When this method is executed, frames will pop up from the stack. All data on the java stack is private, and no other thread can use the stack data of the thread.
Variable data of some basic types defined in functions and referenced variables of objects are allocated in the function stack memory.
WhenCodeWhen a block defines a variable, Java allocates memory space for the variable in the stack. When the variable exits the scope, Java Automatically releases the memory space allocated for the variable, the memory space can be used by another user immediately.
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 collector of the Java Virtual Machine.
In contrast to the stack, the stack 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, you can define a special variable in the stack so that the value of this variable in the stack is equal to the first address of the array or object in the heap memory, the variable in the stack becomes the referenced variable of the array or object. The referenced variable is equivalent to an array or an object name. Later, you can use the referenced variable in the stack in the program to access the array or object in the heap. The referenced variable is equivalent to an array or object name.
The referenced variable is a common variable. It is assigned to the stack during definition. The referenced variable is released after the program runs out of its scope. Arrays and objects are allocated in the heap. Even if the program runs outside the code block where the new statement is used to generate arrays or objects, the memory occupied by arrays and objects is not released, arrays and objects become junk only when no referenced variable points to it. They cannot be used, but still occupy the memory space, the garbage collector collects (releases) the garbage collector at an uncertain time ). This is also the reason why Java accounts for memory usage.
In fact, the variables in the stack point to the variables in the heap memory. This is the pointer in Java!
the Java heap is a runtime data zone and class (the object allocates space from it. These objects are created using commands such as new, newarray, anewarray, and multianewarray. They do not need program code to be explicitly released. The heap is responsible for garbage collection. The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not have to be told in advance because the heap dynamically allocates memory at runtime, the Java Garbage Collector automatically collects the unused data. but the disadvantage is that the access speed is slow because the memory needs to be dynamically allocated during runtime.
the advantage of stack is that the access speed is faster than that of stack, 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 fixed, and there is a lack of flexibility. The stack mainly stores some basic types of variable data (INT, short, long, byte, float, double, Boolean, char) and object handle (reference ).
a very important feature of stacks is that data in stacks can be shared. Suppose we define both:
int A = 3;
int B = 3;
the compiler first processes int A = 3; first, it creates a reference with the variable A in the stack, and then finds whether there is a value of 3 in the stack. If not, it stores 3 and points A to 3. Then process int B = 3. After the referenced variable of B is created, B is directed to 3 because there is already 3 in the stack. In this way, both A and B point to 3 at the same time.
if A is set to 4 again, the compiler re-searches for four values in the stack. If no value exists, it stores four in, and point A to 4. If yes, direct a to this address. Therefore, changing the value of A does not affect the value of B.
note that the sharing of data is different from the sharing of two objects pointing to one object at the same time, because the modification of a does not affect B, it is completed by the compiler, which helps save space. A variable referenced by an object modifies the internal state of the object, which affects the variable referenced by another object.
Constant pool)
A constant pool refers to some data that is determined during the compilation period and stored in the compiled. Class file. Besides containing the basic types (such as int and long) defined in the Code and the constant values (final) of the object type (such as string and array) it also contains some symbolic references that appear in the form of text, such:
1. Full qualified names of classes and interfaces;
2. Field name and descriptor;
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 for this 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 the constant pool. The constant pool in JVM exists in the memory as a table. For the string type, there is a fixed-length constant_string_info table to store the text string value. Note: this table only stores the text string value, not the symbol reference. Here, we should have a clear understanding of the storage location of string values in the constant pool.
During program execution, the constant pool is stored in the method area rather than in the heap.
Note:ArticleAuthor: published in Java advanced network http://www.javady.com/index.php/32.html