Java memory region

In the world of Java as a virtual machine provides a relatively safe memory management and access mechanism, so as to avoid the most memory leaks and pointer cross-border problems, but still need our Java programmers understand the principle behind the virtual machine, so that when problems arise, not hurry-scurry, don’t even know where to start.

The run-time data area

A simple structure is shown below:

Details to pay attention to:

The method area described in the figure above is just a logical concept, and its implementation varies from Java virtual machine to HotSpot virtual machine. In this article, we mainly focus on the HotSpot virtual machine. Before JDK1.8, we used permanent generation to implement the method area.

1. Program counter

The program counter is mainly done by changing the value of the counter to select the next need to execute bytecode instructions, can be thought of as the current thread performs the number of rows in the bytecode indicator, branch, loop, jump, exception handling, restore the basic function such as thread are dependent on the program counter, it belongs to the thread private, Each thread has its own program counter, which is the only area of Java memory that does not have an OOM (OutOfMemoryError).

2. Java Virtual machine stack

Like the program counter, the Java virtual machine stack is thread-private and has the same lifecycle as a thread. It describes the thread-in-memory model of the execution of Java methods. Each time a method is called, data is wrapped in stack frames and passed through the stack. Stack frame is used to store information such as local variable table, operand stack, dynamic link, method exit and so on. A complete call of method corresponds to the process of stack frame loading and unloading.

3. Local method stack

The native method stack is similar to the Java Virtual machine stack, but unlike the Java virtual machine stack, the native method stack is only used by invoking native methods decorated with native keywords.

4, the Java heap

The Java heap is the largest chunk of memory managed by the virtual machine. It is an area shared by all threads and is used to store object instances. Almost all Java object instances are allocated in this place. Since most garbage collectors are designed based on generation theory, the Java heap can be divided into new generation and old generation in design, with different areas for better garbage collection.

The heap is typically divided into the following areas:

Young Generation: As shown in the Eden area, From and To Survivor belong To the new Generation

The Old Generation

Most objects are allocated in Eden first. After a garbage collection, the surviving objects will enter From or To Survivor, and the age of the object will be +1. After a certain number of ages (the default promotion value for the CMS garbage collector is 6), the old age is entered.

As the number of objects in the heap increases, overflow exceptions will occur when the total capacity of the maximum heap reaches the capacity limit. You can set the maximum and minimum value of the heap with the parameters -xms and -xmx.

5. Method area

The method area is also an area of memory shared by each thread. It is mainly used to store data such as type information, constants, static variables, and code cache compiled by the just-in-time compiler that has been loaded by the virtual machine.

Run time constant pool

The runtime constant pool is part of the method area. In addition to the description of the Class version, fields, methods, interfaces, etc., the Class file also contains the constant pool table (used to store various literal and symbolic references generated at compile time).

Since the runtime constant pool is part of the method area and is naturally limited by the method area memory, OutOfMemoryError is thrown when the constant pool can no longer claim memory.

7. Direct memory

Direct memory is not managed directly by the virtual machine, but is frequently used. Its size is not limited by the Size of the Java virtual machine, but is limited by the size of the native memory, and can cause OutofMemoryErrors.

Second, the object

1. Object creation

Class loading check

When a virtual machine receives a new instruction, it first checks to see if the instruction’s parameters can locate the symbolic reference of the class in the constant pool, and if the class represented by the symbol has been loaded, parsed, and initialized. If not, it must perform the corresponding class loading process first

Allocate memory

After the class load passes, the virtual machine next allocates memory for the new objects. The required size of an object can be fully determined once the class is loaded. Allocating space for an object is the same as dividing a block of memory of a certain size from the Java heap in one of the following ways:

Pointer to the collision The free list
Usage Scenarios: Memory is neat The memory is irregular and has many memory fragments
The principle of The used memory is placed on one side, and the free memory is placed on the other side, with a pointer in the middle as an indicator of the demarcation point. Move the pointer to the free side by the same size as the object The virtual machine maintains a list of memory blocks that are available, finds a large enough space to allocate to object instances, and then updates the list
The specific application Serial, ParNew garbage collector CMS garbage collector

Initialize a zero value

After the memory allocation is complete, the virtual machine initializes the allocated memory space (excluding the object header) to zero values. This operation ensures that the object instances can be used directly in Java code without assigning initial values, so that the program can access the zero values corresponding to the data types of these fields.

Set the object header

In the previous step after the initialization of the Java virtual opportunity to set of objects, for example, which is the object instance of a class, how can you find the class metadata information of object, the object’s hash code, GC generational information such as age, the information will be stored in the object object head, in addition, according to the current state of the running virtual machine, For example, whether biased locking is enabled or not, the object header can be set in different ways.

Execute the init method

After all the above steps are done, a new object has been created from the virtual machine’s point of view, but from the Java program’s point of view, the creation of the object has only just begun. The () method in the Class file has not been executed, all fields have default zero values, and other information is not constructed as intended. The new instruction is followed by the () method, which initializes the object as the programmer wants it to, until a real object is fully constructed.

Object memory layout

In the HotSpot VIRTUAL machine, the storage layout of objects in the heap memory can be divided into three parts: object headers, instance data, and aligned padding.

The object header of the HotSpot VIRTUAL machine object contains two types of information: the first type is the object’s own runtime data (Mark Word) including hash code, GC generational age bias lock ID, etc. The second type is a type pointer, which is a pointer to an object’s type metadata that the Java virtual machine uses to determine which class the object is an instance of. The instance data section stores the actual valid information of the object, which is the content of the various types of fields that we define in the program code.

2. Object access location

Objects are created to use objects, and Java programs manipulate specific objects on the heap using reference data on the stack. To operate an object, you must first access the object. There are two main ways to access the object: handle access and direct pointer access:

1, ** handle access: **Java heap may be divided into a memory as a handle pool, reference is stored in the address of the object handle, handle contains the specific address information of the object instance data and type data

2. Direct pointer access: With direct pointer access to objects, the memory layout of objects in the Java heap must consider how to place the information related to the access type, and the direct stored in reference is the object address.

The advantage of handle access is that reference stores the stable handle address. When the object is moved, only the instance data pointer in the handle will be changed, and reference itself does not need to be modified. The advantage of using direct pointer access is that the access speed is faster and the time cost of pointer location is saved. Direct pointer access is mainly used in HotSpot VIRTUAL machines.

Reference:

  • Understanding the Java Virtual Machine in Depth: Advanced JVM Features and Best Practices (3rd Edition)

Public account: Java Yu Feng