[JVM]-[In-depth understanding of Java virtual machine study notes]-Chapter 2-Java memory area

Preface

The main components of JVM

JVM includes two subsystems and two components . The two subsystems are Class Loader and Execution Engine ; the two components are Runtime Data Area and Native Interface.

The class loader loads the Class file with the given fully qualified class name into the method area of ​​the runtime data area.
The execution engine executes the instructions in the class file.
The execution process may need to call the local interface , which is used to interact with the local library.
The runtime data area is Commonly mentioned JVM memory

The role of JVM

First, the Java code is converted into bytecode through the compiler. The class loader loads the bytecode into memory and places it in the method area. The bytecode file is just a set of instruction set specifications of the JVM and cannot be directly It is handed over to the underlying operating system for execution, so a specific command parser execution engine is needed to translate the bytecode into underlying system instructions, and then handed over to the CPU for execution. This process will call native library interfaces in other languages ​​​​to realize the functions of the entire program.

Java memory area

Overview

For C/C++ developers, they have the "ownership" of each object (the area obtained by space allocation functions such as malloc), and are responsible for the maintenance of each object from the beginning to the end of its life (using functions such as delete and free). ), and with the help of the virtual machine's automatic memory management mechanism, Java programmers do not need to write paired delete/free code for each new operation, and are not prone to memory leaks and memory overflow problems; but on the other hand, it is Because the right to control memory is given to the Java virtual machine, once problems such as memory leaks and memory overflows occur, if you do not understand how the virtual machine uses memory, it will be extremely difficult to find errors and solve problems. Therefore, it is necessary to content to learn

Memory overflow: It can be understood that the memory space is occupied too much, resulting in that when a program wants to apply for memory, there is not enough memory to apply for it. Memory leak: It can be understood that after
some areas in the memory are applied for, because For some reasons, it is not released, and other programs cannot apply for this part of the space, causing the memory space to be wasted. Eventually it will lead to memory overflow

Runtime data area

Insert image description here

  • Program counter : It can be regarded as a line number indicator of the bytecode executed by the current thread. When the bytecode interpreter works, it selects the next bytecode instruction to be executed by changing the value of this counter.

    Since the multi-threading of the virtual machine is implemented by switching threads in turns and allocating processor execution time, at any given moment, a CPU (or a core) will only execute instructions in one thread. In order to The correct execution position can be restored after thread switching. Each thread has an independent program counter.

    This memory area is the only one that does not specify any OutOfMemoryError in the Java Virtual Machine SpecificationO u tO f M e m ory Error area _

  • Virtual machine stack : Thread private, the life cycle is the same as the thread. Describes the thread memory model of Java method execution: when each method is executed, the virtual machine synchronously creates a stack frame to store local variable tables, operand stacks, dynamic connections, method exits and other information, from when the method is called to execution Completed, corresponding to the push to pop of a stack frame

  • Local method stack : Similar to the virtual machine stack, the difference is that the virtual machine stack executes Java method (bytecode) services for the virtual machine, while the local method stack uses local method (native) services for the virtual machine.

  • Heap : shared by all threads, created when the virtual machine starts. Its sole purpose is to store object instances. The heap is a memory area managed by the garbage collector, also known as the "GC heap"

  • Method area : shared by each thread. Used to store class information, constants, static variables, code cache compiled by the just-in-time compiler and other data that have been loaded by the virtual machine.

  • Runtime constant pool : It is part of the method area. In addition to the description information such as class version, fields, methods, interfaces, etc., there is also a constant pool table in the Class file, which is used to store various literals generated during compilation. Referenced with symbols, this part of the content will be stored in the runtime constant pool in the method area after the class is loaded.

  • Direct memory : The NIO (New Input/Ouput) class was added to JDK1.4, and an IO method based on channels and buffers was introduced. It can use the Native function library to directly allocate off-heap memory, and then store it in Java through a The DirectByteBuffer object in the heap operates as a reference to this memory, which can significantly improve performance in some scenarios because it avoids copying data back and forth between the Java heap and the Native heap.

Object creation, layout, access

Object creation

  • When the virtual machine encounters a bytecode new instruction, it first checks whether the parameters of this instruction can locate a symbolic reference of a class in the constant pool, and checks whether the class represented by this symbolic reference has been loaded, parsed and initialized. If not, execute itclass loadingprocess
  • After the class loading check passes, memory must be allocated for the new object. Allocating space for the object is actually equivalent to dividing a memory block of a certain size from the Java heap . There are two methods of space allocation: pointer collision and free list . Which method is chosen depends on whether the Java heap is regular, and whether the Java heap is regular or not depends on the method used.garbage collectorDetermine whether it has space compression and organization capabilities.SerialPar NewThe collector with compression and sorting process uses pointer collision, which is simple and efficient;CMSThe collector is based onclearing algorithm, in theory, a more complex free list is used for allocation. The reason why I say theoretically is because in the implementation of CMS, in order to allocate faster in most cases, a called Linear Allocation Buffer Linear Allocation Buffer is designed.L in e a r A ll oc a t i o n Buffer 's allocation buffer. After getting a large allocation buffer through the free list, it can still be allocated using pointer collision .
  • Object creation is a very frequent behavior, so it is not thread-safe in concurrent situations. There are two solutions: one is to synchronize the operation of allocating space, and the virtual machine uses CAS with failure retry ; the other is to divide the memory allocation action into different spaces according to threads, that is, each thread is in Java First allocate a small piece of memory in the heap, called the local thread allocation buffer Thread Local Allocation Buffer . Threads allocate in their respective allocation areas, so thread problems will not occur. Only when the local buffer is used up, a new buffer must be allocated. Synchronization lock is required only when the area is

Object memory layout

The storage layout of objects in heap memory can be divided into three parts: object header , instance data , and alignment padding.

Object header

Contains two types of information: its own runtime data and type pointer

  • The runtime data of the object itself: such as hash code , GC generation age , lock status flag , and lock held by the thread . The length of this part of data is 32 bits and 64 bits respectively in the virtual machines of 32-bit and 64-bit machines. It is officially called Mark Word. Taking into account the space efficiency of the virtual machine, Mark Word is designed to be a dynamic definition. Data structure to store as much data as possible in a very small space and reuse its own storage space according to the state of the object
  • Type pointer: The object points to its type metadata. The virtual machine uses this pointer to determine which class the object is an instance of.
  • If the object is an array, there must be a memory used to record the data length in the object header. This memory only occupies 4B, so the maximum length that can be applied for a Java array is 2 32

Instance data section

The effective information actually stored by the object, that is, the various types of fields defined in the code

Align padding

HotSpot requires that the object starting address must be an integer multiple of 8 bytes, which means that the size of any object must be an integer multiple of 8 bytes, and the object header has been carefully designed to be a multiple of 8 bytes, so if the instance data If the part is not aligned, it must be filled in by alignment and filling.

Object access

The reference type data we usually use is stored in the stack, which is a reference pointing to the object. To access the object through this reference, there are two mainstream access methods: handle and direct pointer . In the handle mode, the reference points to the handle, and then the handle points to the instance data and type data of the object. Therefore, when accessing the object, the reference must first locate the handle, and then further locate the position of the object. The handle address is stable, and when the object is moved Only the pointer to the object instance data in the handle will be modified, and the reference itself does not need to be modified ; the advantage of using direct pointers is that it saves the time overhead of pointer positioning. Since object access is very frequent in Java, this type of overhead accumulates. Less for more is also a significant execution cost . So the direct pointer method is used in the JVM

Guess you like

Origin blog.csdn.net/Pacifica_/article/details/124430395