Jvm memory area and class loading process

Runtime data area

Program counter

​ is private to the thread. The program counter is a small piece of memory. It can be regarded as a symbolic instruction of the bytecode executed by the current thread. It is an indicator of program control flow, branching, looping, jumping, and exception handling. Basic functions such as, thread recovery, etc. all need to rely on this counter to complete.

Java virtual machine stack

​ is private to the thread. When each method is executed, a stack frame (an important basic data structure when the method is running) is called to store information such as the local variable table, operand stack, dynamic link, and method exit. After each method is called, it is considered the process of this stack frame from popping to stacking. The local variable table stores the basic types, object references, and returnAddress types of various Java virtual machines known at runtime (pointing to the address of a bytecode instruction). The storage space of these data types in the local variable table is represented by a local variable slot. The 64-bit long and double occupies two slots, and the others occupy one slot.

Native method stack

	为虚拟机栈使用到本地的方法服务 

Java heap

	线程共享区,是虚拟机中所管理的最大的一块区域,此堆的唯一目的就是在jvm启动的时候存放实例。几乎所有的实例对象都在这分配内存。是垃圾回收器管理的内存区域,所以通常来说还分为新生代(From Survivor、To Survivor、Eden空间)、老年代,这样能够更好的回收内容。配置Java堆的大小可以通过参数-Xmx和-Xms设定。如果Java堆中没有内存完成实例的分配,且无法再进行扩展,这时jvm就将抛出OOM异常。

Method area

​ Thread shared area. It is used to store data such as type information, constants, static variables, and code cache after just-in-time compilation that have been loaded by the jvm. The method area before jdk8 is also called permanent generation. However, after 1.8, jvm completely abandoned the concept of permanent generation and replaced it with meta-space implemented in local memory. Move all the remaining content (mainly type information) in jdk7 from the permanent generation to the meta space. If the method area cannot meet the new memory allocation requirements, an oom error will be thrown.

Runtime constant pool

	是方法区的一部分,class文件除了有版本、字段、方法、接口等信息外,还有一个常量池表,用于存放编译时产生的各种字面量和符号引用。当常量池无法再分配到内存时会抛出oom error。 

The way the class loads

image-20200501001426467

Now let's describe the process of class loading in words:

​ Generally speaking, the way to create an object is the new keyword (others such as clone, deserialization). When the virtual machine interprets the new instruction, it will first check whether the parameter of this instruction exists in the constant pool, and if it exists, it will Determine whether it has been loaded, parsed, and initialized. If not, class loading will be performed.

​ Next, memory will be allocated in the heap, and memory allocation is also divided into two situations. The first is that the memory is in a regular state. In this case, the pointer of the memory area will allocate an area as large as the object. Another kind of irregularity, that is, the used area and the unused area overlap. At this time, the virtual machine will maintain a list to store the available area. When the memory is allocated, a large enough area can be found from the list to be allocated to the instance.

​ Of course, this is only under normal circumstances, under concurrent circumstances, it is not thread-safe. The problem of ABA is prone to occur, here jvm provides two solutions. The first type: Synchronize the action of allocating space, that is, to ensure thread safety through CAS + failure retry. The second type: the virtual machine divides different operating areas according to threads (local thread allocation buffer: TLAB). When each thread performs memory allocation, it will first allocate in the divided operating area. When this buffer area is allocated After that, perform the synchronization operation again.

​ When the memory is allocated, the virtual machine will initialize the allocated memory space (including the object header) to 0.

Object access location

Divided into two ways:

  • Handle access

    ​ For handle access, the Java heap may be divided into an area as the handle pool, and the reference type stores the handle address of the storage object. The second handle contains the specific addresses of the object instance data and data types.

    The advantage is that the reference stores a stable handle address, which will not be affected by the object moved by the garbage collector. It will only change the instance data pointer in the handle, and the reference does not need to be modified.

  • Pointer access

    The memory layout of the object in the Java heap must consider how to place the relevant information about the access type data, and the object address is directly stored in the reference. If you directly access the object itself, there is no need for an additional indirect access overhead.

    The biggest advantage is faster speed. Since accessing objects is very frequent in Java, the speed savings are also considerable.

References: "In-depth understanding of the Java virtual machine"

Guess you like

Origin blog.csdn.net/pengyiccb/article/details/105873199