JAVA memory management and detailed explanation of each memory area

1、 Overview

Original link: http://blog.csdn.net/l271640625/article/details/39761439

In the process of executing Java programs, Java virtual machine will divide the memory it manages into several different data areas, which have their own purposes and the time of creation and destruction. The memory managed by the Java virtual machine will include the following runtime data areas, as shown in the following figure:

Each area is described below.

2、 Runtime data area

Program counter

The program counter can be regarded as the line number indicator of the bytecode executed by the current thread. In the conceptual model of virtual machine, the work of bytecode interpreter is to select the next bytecode instruction to be executed by changing the value of program counter. The basic functions such as branch, loop, jump, exception handling and thread recovery all depend on this counter.

In multithreading, in order to recover to the correct execution position after thread switching, each thread needs to have an independent program counter. Each thread does not affect each other and is stored independently. Therefore, this memory is private to the thread.

When the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed; When the native method is executed, the counter value is empty.

This memory area is the only area that does not specify any outofmemoryerror condition.

Java virtual machine stack

Java virtual machine stack is also thread private, and its life cycle is the same as that of thread. Virtual machine stack describes the memory model of Java method execution: each method will create a stack frame to store local variable table, operand stack, dynamic linked list, method exit information, etc. The process from calling to execution of each method corresponds to the process of putting a stack frame into and out of the virtual machine stack.

The local variable table stores various basic data types known to the compiler (Boolean, byte, char, short, int, float, long, double), object reference and returnaddress types (pointing to the address of a bytecode instruction).

If enough memory cannot be requested during expansion, an outofmemoryerror exception will be thrown.

Native Method Stack

The function of the local method stack is similar to that of the virtual machine, except that the virtual machine stack serves the Java methods executed by the virtual machine, while the local method stack serves the native methods used by the virtual machine. Some virtual machines directly combine the local method stack with the virtual machine stack.

Stackoverflowerror and outofmemoryerror exceptions are thrown.

Java heap

Java heap is a memory area shared by all threads. It is created when the virtual machine starts. The only purpose of this memory area is to store object instances.

The Java heap is the main area managed by the garbage collector. Since the collector basically adopts the generational recycling algorithm, the Java heap can also be divided into the new generation and the old generation. From the perspective of memory allocation, the Java heap shared by threads may be divided into multiple thread private allocation buffers (tlabs).

Java heap can be in physically discontinuous memory space, as long as it is logically continuous. In the implementation, it can be fixed or extended.

If there is no memory in the heap to complete the instance allocation and the heap cannot complete the expansion, an outofmemoryerror exception will be thrown.

Method area

The method area is a memory area shared by each thread. It is used to store class information, constants, static variables, code compiled by the real-time compiler and other data that have been loaded by the virtual machine.

Relatively speaking, garbage collection rarely occurs in this area, but it does not exist permanently when the data enters the method area. The memory recycling goal of this area is mainly for the recycling of constant pools and the unloading of types,

When the method area cannot meet the memory allocation requirements, an outofmemoryerror exception will be thrown.

Runtime constant pool:

Is a part of the method area, which is used to store various literal and symbolic references generated at compile time.

Direct memory

Direct memory is not a part of the virtual machine runtime data area. An IO method based on channel and buffer is introduced into NiO class. It can directly allocate out of heap memory using the native function library, and then operate through a directbytebuffer object stored in the Java heap as a reference to this memory.

Direct memory allocation will not be limited by the Java heap size, but will be limited by the native memory size. All may throw outofmemoryerror exceptions.

3、 Object creation, layout and access process

Object creation

Creating an object usually requires the new keyword. When the virtual machine encounters a new instruction, first check whether the parameters of the instruction locate the symbol reference of a class in the constant pool, and check whether the class represented by the symbol reference has been loaded, parsed and initialized. If so, perform the corresponding class loading process.

After the class load check passes, the virtual machine will allocate memory for the new object. The task of allocating space for objects is equivalent to dividing a certain size of memory from the Java heap. There are two allocation methods: one is called pointer collision. Assuming that the memory in the Java heap is absolutely regular, the used and free memory are on one side, and a pointer is placed in the middle as the indicator of the dividing point. Allocating memory is to move the pointer to the free space for a distance equal to the size of the object. The other is called free list: if the memory in the Java heap is not regular, the virtual machine needs to maintain a list to record which memory block is available, find a large enough space from the list to allocate to the object instance, and update the records on the list. The allocation method is determined by whether the Java heap is regular or not, and whether the Java heap is regular or not is determined by whether the garbage collector used has compression and collation function. Another problem that needs to be considered is thread safety during object creation. There are two solutions: one is to synchronize the actions of allocating memory space; The other is that the memory allocation is performed in different spaces according to the thread division, that is, each thread pre allocates a small piece of memory (TLAB) in the Java heap. Which thread needs to allocate memory is allocated on the TLAB of which thread. Synchronous locking is required only when the TLAB is used up and a new TLAB is allocated.

After the memory allocation is completed, the virtual machine needs to initialize the allocated memory space to zero. This step ensures that the instance field of the object can be used directly without assigning an initial value in Java code.

Next, the virtual machine should make necessary settings for the object, such as which class instance the object is, how to find the metadata information of the class, etc. these information are stored in the object header of the object.

After the above work is completed, a new object has been generated from the perspective of virtual machine. However, from the perspective of Java programs, you also need to execute the init method to initialize the objects according to your wishes, so that a truly available object can be fully generated.

Object's memory layout

In the hotspot virtual machine, the layout of objects stored in memory can be divided into three parts: object header, instance data and alignment padding.

The object header includes two parts: the first part is used to store the runtime data of the object itself, such as hash code, GC generation age, lock held by thread, etc. It is officially called "mark word". The second part is the type pointer, that is, the pointer of the object to its class metadata. The virtual machine uses this pointer to determine which class instance the object is.

Instance data is not only the effective information stored by the object, but also the content of various types of fields defined in the program code.

Aligned padding is not necessary, it just acts as a placeholder The hotspot VM requires that the object start address must be an integer multiple of 8 bytes, and the object header is exactly a multiple of 8 bytes. Therefore, when the instance data part is not aligned, it needs to be aligned through alignment filling.

Object access location

Java programs manipulate specific objects on the heap through the reference data on the stack. The main access methods are handle and direct pointer:

Handle: the Java heap will set aside a piece of memory as the handle pool. The reference stores the handle address of the object, and the handle contains the specific address information of the object instance data and type data. As shown in the figure:

Direct pointer: the layout of Java heap objects should consider how to place the relevant information of access type data. The object address is stored in the reference. As shown in the figure:

The two methods have their own advantages. The biggest advantage of using a handle is that the stable handle address is stored in the reference. When the object is moved, it will only change the address of the instance in the handle. The reference does not need to be modified. The advantage of using direct pointer access is faster, which saves the time cost of finger positioning.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>