First knowledge of the java memory area, first knowledge of the java area
Directory:
1. Data Area During Running
2. Create an object
3. Memory layout of Objects
4. Access and positioning of Objects
I. runtime data area
The basic data zone of the Java Virtual Machine is as follows:
Next, let's take a look at these runtime data regions one by one.
1. Program Counter (Program Counter Register)
It is a relatively small memory and can be seen as the indicator of the bytecode line number executed by the current thread. Each thread requires an independent thread counter, and the counters between threads do not affect each other, independent storage. Therefore, this type of memory is called "thread-private" memory.
Scenario:
1) when the thread is executing a java method, this counter records the address of the Virtual Machine bytecode instruction being executed.
2) If the Native method is being executed, the counter value is null.
2. Java Virtual Machine Stacks)
The data zone is also "thread private", and its lifecycle is the same as that of the thread. The Virtual Machine stack describes the Memory Model of Java execution. Each method creates a stack frame during execution to store information such as the local variable table, operand stack, dynamic link, and method exit. The process from calling to execution of each method corresponds to the process from a stack frame in the virtual machine to the stack.
We usually get used to dividing Java memory into heap memory and memory occupation. This method is rough, and the actual memory area is far more complex. The stack mentioned here is our virtual machine stack.
3. Native Method Stack)
The role is similar to that of the Virtual Machine stack. The difference between them is that the virtual machine stack executes Java methods or bytecode services for the virtual machine, the local method stack is the Native method service used by virtual machines.
4. Java Heap)
Java heap is a memory area shared by all threads. It is created when a VM is started to store object instances. Almost all objects are allocated here (but with the development of technology, "all" is not so absolute)
Java heap is the main management area of the garbage collector, also called the "GC" heap.
From the perspective of memory collection, Java heap can be divided into "new generation" and "Old Generation"
5. Method Area)
It is also a storage area shared by various threads. It is used to store data such as class information, constants, static variables, and Code Compiled by the real-time compiler.
6. Runtime Constant Pool)
It is part of the Method Area. In addition to the class version, field, method, interface, and other description information, the class file also has a constant pool, stores various literal and symbolic references generated during the compilation period. This part of content will be stored in the method zone runtime pool after the class is loaded.
7. Direct Memory)
It is not part of the data zone during virtual machine running, nor the memory area defined in the Java Virtual Machine specification. However, this part of memory will be frequently used.
Ii. Object Creation
1. The creation of objects at the language level is just a new Keyword, but the creation of objects in virtual machines is complicated.
2. when the VM encounters a new command, it first checks whether the parameter of this command can locate a symbolic reference of a class in the constant pool, check whether the class referenced by this symbol has been loaded, parsed, and initialized. If no, the corresponding class loading process must be executed first. After the class is loaded, the VM will allocate memory for the object, the memory size required by the object can be fully determined after the class is loaded (how to determine the size will be mentioned in the memory layout section of the object later ), the task of allocating space for objects is equivalent to dividing a fixed-size memory from the java heap.
3. there are two main ways to allocate space. The method is determined based on whether the Java heap is regular, and whether the Java heap is regular depends on whether the Garbage Collector has the compression and sorting function.
1) Pointer collision (Bump the Pointer): the memory in the Java heap is absolutely regular, all used memory is put on one side, and idle memory is put on the other side, there is a pointer in the middle as the indicator of the demarcation point, and the allocated memory is to move the pointer to the other side of the free space to a distance equal to the object size.
2) Free List: the memory in Java is not regular, and the used memory and idle memory are intertwined, so there is no way to perform a pointer collision, A virtual machine must maintain a list to record the items that can be used quickly. During redistribution, a large enough space is found in the list and allocated to the object instance, and records on the list are updated.
4. after the memory is allocated, the virtual machine needs to initialize the allocated memory space to 0 (excluding the object header). If the TLAB (Local Thread Allocation Buffer Thread Local Allocation Buffer, A small block of memory pre-allocated by each thread in the Java heap). This work process can also be performed in advance when TLAB is allocated. This step ensures that the instance field of the object can be directly used in Java code without assigning an initial value. The program can access the zero value corresponding to the Data Type of these fields.
5. next, you need to set the virtual machine as the object, for example, the instance of the class of the object, how to find the metadata information of the class, the hash code of the object, and the GC age of the object. The information is stored in the Object Header. The object header has different settings depending on the current running status of the virtual machine.
6. after the above steps are completed, the object has been created from the perspective of the virtual machine, but from the perspective of the Java program, the object has just been created, and the init initialization has not been executed yet, execute the new command and then execute the init method. After the object is initialized according to our own wishes, an available object is created completely.
3. Object Memory Layout
In the HotSpot virtual machine, the layout of objects stored in the memory can be divided into three areas: object Header, Instance Data, and Padding)
1. the object header contains two parts. The first part is used to store the data during running of the object, such as hash code, GC generational age, lock status mark, lock held by the thread, biased thread ID, biased timestamp, etc. Another part is the type pointer, that is, the pointer of the object to its class metadata. The VM uses this pointer to determine the instance of the class of the object. Not all Virtual Machine implementations must retain the type pointer on the object data. In other words, the metadata information of the object does not have to pass through the object itself. In addition, if the object is a Java array, there must be a piece of data in the object header to record the length of the array, the virtual machine can determine the size of a Java object through the metadata information of a common Java object, but the size of an array cannot be determined in the metadata of an array.
2. instance data, which is the valid information actually stored and the various types of field content defined in the program code
3. alignment filling does not exist, and it has no special meaning. It only exists to play a placeholder role, because the Automatic Memory Management System of the HotSpot virtual machine requires that the starting address of the object must be an integer multiple of 8 bytes. In other words, the object size must be an integer multiple of 8 bytes, the object header is exactly an integer multiple of 8 bytes. Therefore, when the data part of the object instance is not aligned, the object needs to be filled with alignment.
Iv. Access and positioning of Objects
1. objects are created to use objects. Our Java program uses reference data on the stack to operate specific objects on the stack. In the Java Virtual Machine specification, only one reference pointing to an object is specified, it does not define how the reference locates and accesses the specific location of the target object. Therefore, the object access method depends on the Virtual Machine implementation. Currently, there are two mainstream access methods:
1) if a handle is used for access, a memory block will be allocated in the Java heap as the handle pool. The handle address of the object is stored in the reference, the handle contains the specific address information of the object instance data and type data.
2) If direct pointer access is used, the layout of the Java heap object must consider how to place the basic information of the textbook data type, and the object address stored in reference is directly
2. Comparison of the two access methods:
1) The biggest advantage of using a handle is that the reference stores a stable handle address. when an object is moved, only the instance data pointer in the handle is changed, and the reference itself does not need to be modified.
2) The biggest advantage of direct access is faster speed, which saves the time overhead of one pointer location.
Supplement:
This blog is a summary of "deep understanding of Java Virtual Machine". It summarizes some of the most important and easy-to-understand parts in Chapter 2, at the same time, I have deepened my understanding of the Java memory area. In short, I hope this blog will help you understand the Java memory area.
Your support is the greatest help for bloggers to learn and think deeply.