One, Java virtual machine memory prototype
Register : We can't control it in the program.
Stack : a reference to a primitive type of data and objects, but the object itself is not stored in the stack, but is stored in the heap.
Heap : Stores data generated with new.
static domain : A static member stored in the object that is defined with static.
constant Pool : Holds constants.
non-RAM storage : Persistent storage space such as hard drives.
Second, Chang (constant pool)
Chang refers to some data that is determined at compile time and is saved in the compiled class file.
In addition to the constant values (final) that contain the various basic types defined in the code (such as int, long, and so on) and object types (such as strings and arrays), there are some symbolic references that appear as text, such as:
1. The fully qualified name of the class and interface;
2, the name and descriptor of the field;
3. The name and descriptor of the method.
The virtual machine must maintain a constant pool for each mounted type. A constant pool is an ordered set of constants used by that type, including direct constants (String,integer, etc.) and other types: symbolic references to 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 there is a fixed-length constant_string_info table for storing literal string values, note that the table stores only literal string values and does not store symbol references. In this case, there should be a clearer understanding of where the string values in the constant pool should be stored. When the program executes, the constant pool is stored in the Method area, not the heap.
third, Java memory allocation in the stack
The base unit of the stack is the frame (or stack frame): Whenever a Java thread runs, the Java virtual opportunity allocates a Java stack for 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, intermediate operation results, and so on. When this method executes, the frame pops up from the stack. All data on the Java stack is private, and other threads cannot access the thread's stack data.
Some basic types of variable data and object reference variables that are defined in the function are allocated in the function's stack memory. 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 frees the memory space allocated for the variable, and the memory space can be immediately
He used.
Iv. Heap in Java memory allocation
The heap in the Java virtual machine is used to hold the 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 to manage the heap's memory. The simple say and stack is relatively, the heap is mainly used to store Java objects, the stack is mainly used to store object references. After generating an array or an object in the heap, you can also define a special variable in the stack that is equal to the first address of the array or object in the heap memory, and this variable in the stack becomes the reference variable of the array or object. A reference variable is a name that is an array or an object, and you can use reference variables from the stack to access the arrays or objects in the heap later in your program. A reference variable is equivalent to a name that is an array or an object.
A reference variable is a normal variable that is allocated in the stack when defined, and the reference variable is freed after the program runs outside its scope. While the array and the object itself are allocated in the heap, the memory occupied by the array and the object itself is not freed when the program runs beyond the block of code that uses new to produce the array or the object's statement, and the array and the object become garbage when no reference variable points to it, and are no longer used, but still occupy memory space. The garbage collector takes off (releases) at a later indeterminate time.
This is why the Java comparison accounts for 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 run-time data area in which the objects of the class allocate space. These objects are created through directives such as the New + constructor, which do not require program code to be explicitly released. Heap is responsible for garbage collection, the advantage of the heap is the ability to dynamically allocate memory size, the lifetime does not have to tell the compiler beforehand, because it is at runtime to allocate memory dynamically, Java garbage collector will automatically take away these no longer use data. However, the disadvantage is that the access speed is slower 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. However, the disadvantage is that the size and lifetime of the data in the stack must be deterministic and inflexible. The stack mainly contains some basic types of variable data (Int,short,long,byte,float,double,boolean,char) and object references.
Stack has a very important particularity, is that there is data in the stack can be shared. Suppose we define at the same time: int a=3;int b=3; the compiler processes int a = 3 First, it creates a reference to a variable in the stack, and then finds the value of 3 in the stack, if not found, stores the 3 in, then points a to 3, then int b = 3; After creating the reference variable of B, since there are already 3 values in the stack, point B directly to 3 so that A and B both point to 3.
At this point, if the a=4 again, then the compiler will re-search the stack for 4 values, if not, then 4 is stored in, and a point to 4, if already, then direct a to the address. Therefore the change of a value does not affect the value of B.
It is important to note that this sharing of data with a reference to two objects simultaneously points to an object whose modification does not affect B, which is done by the compiler, which facilitates space saving. An object reference variable modifies the internal state of the object , affecting another object reference variable.
Deep parsing of Java memory prototypes