JVM Series - in-depth understanding of JVM memory area

Foreword

The Java .classfile through the class loader loads into memory inside a virtual machine, JVM by parsing executed by a virtual machine, or compile implementation. In order to facilitate the management of JVM come loaded .classcontent, the concept of the Java Virtual Machine runtime data area. Java virtual machine runtime data area can be divided into private threads, threads share the two types of data area, which includes a program counter thread private, virtual machine stack, native method stacks; and threads share including Java heap, the method area.

In the absence of in-depth understanding of JVM, Java Runtime we will often coarse-grained data area divided into a "heap", "stack" of two parts, "heap" is used to store the object is instantiated, and "stack" is used to store the object references. With our in-depth study on the JVM, we found that the division of JVM memory than we in the initial stage of the cognitive learning Java run-time data area to be complicated. C / C ++ programmers need to manually release program which does not need to reuse memory space, while in Java, the virtual machine automatically help us reclaim resources do not need to use, which requires us to understand the JVM runtime memory division, will help us have a more profound understanding of the program.

Detailed data area

Program Counter

Program counter (Program Counter Register) is a relatively small data area. Because Java is to support multi-threaded, it means that within each thread needs a memory space, the recording time of the current thread switching (on-site destruction), line number of the current byte code execution, in order to re-acquire the thread By the time CPU execution time, bytecode can then be executed last number of rows to continue (site restoration). And the program counter is to record the number of rows bytecode execution of the current thread number proposed.

Parser bytecode is a need to resolve the value of the counter is selected depending on the execution of the bytecode instructions. Circulation Java code, jump, exception handling, thread recovery and so need to rely on the counter to complete.

If the thread is executing a Java method, the counter records the address of the virtual machine bytecode instruction being executed; native method is being performed if the value of this counter is null (Undefined). The program counter is the only one JVM does not provide for any area OOM situation.

VM stack

Virtual Machine stack (Java Virtual Machine Stacks) is also a thread private, its life cycle and the same thread. Is a virtual machine stack memory model described Java method executed: each will execute a method in Java to create a stack frame, then this system will stack frame pushed virtual machine stack. Stack frame may also be divided into a local variable table, the operand stack, dynamic link, the method outlet (Method return address), as well as additional extensions. Java execution method corresponds to the process of pushing and popping the stack frame in the virtual machine.

  • Local variable table is stored in the main method of local variables during execution. The eight basic types, local variables defined in the process.

  • Operand stack is stored in the operand. Operand stack data structure is a stack structure, the stack of elements may be any Java data types. When beginning a method of allocating stack space frame is performed, the operand stack is empty, when the operation of the local variables in a method of performing, when it performs the operand stack push / pop operations.

  • The main role is to support dynamic linking polymorphisms Java language (needs class loading method to determine the runtime), dynamic.

  • Method returns the address returned is the result of return method. If a normal return, the program counter is the address as a return call; if the exception is returned, it is (non-stack frame) is determined by the exception handler table.

At the same time, Java virtual machine specification defines a virtual machine stack has two anomalies:

abnormal definition
StackOverFlowError Throws stack frame when the stack depth exceeds the depth of the virtual machine requests a start of the current stack frame defined
OutOfMemoryError If the virtual machine can dynamically apply the stack memory at run time, when the Java Virtual Machine can not apply to more stack memory Throws

Native method stacks

Quite similar to native method stacks (Native Method Stack) with the virtual machine stack when the role, but the virtual machine stack memory provides space for the common methods Java, and native method stacks compared to Java native method of providing memory space. Therefore, some virtual machines such as Sun HotSpot virtual machine directly to the virtual machine and native method stacks into one stack.

At the same time, native method stacks and stack as a virtual machine, can also throw StackOverFlowError, OutOfMemoryErrortwo kinds of exceptions.

Java heap

Java Virtual Machine heap is the largest piece of memory space, but also the memory area can be shared by each thread. Java heap is created when the virtual machine is created, its main purpose to store an object instance, an array of data. With the development of the JIT compiler, the development of escape analysis techniques, such objects can be allocated on the stack.

Java heap is the main area GC garbage collection. From the perspective of memory recovery, mainly because the Java heap and take From Survivor To Survice space generational collection algorithm, which can be divided into the new generation of Java heap, years old, and the new generation and can be divided into Eden space.

From the perspective of shared memory, Java heap can be divided into multiple threads share allocated buffer (Thread Local Allocation Buffer, TLAB).

The virtual machine specification, Java stack may be discontinuous physical memory, and in the logically contiguous. At the same time, the strength of the heap allocation can not be completed, and the virtual machine can not apply to heap more time, will throw OutOfMemoryErroran exception.

In a virtual machine, the main object instance is to allocate the Java heap, object instances in memory layout is as follows:

Memory layout object instance is divided into three areas: object header (Header), instance data (Instance), alignment padding (Padding).

  • Object header (Header) including two pieces of information in the HotSpot: means for storing runtime data object itself, the type of pointer. For a first portion for storing runtime data itself as shown above; and for the second type of section pointer, i.e. a pointer to the object of its metadata, the virtual machine is determined by the object that the pointer of a class which it belongs. However, not all types of virtual machines need to keep a pointer in the object header data above, since the target of the search is not necessarily required by the metadata object itself (reflection).

  • Examples of the data portion is valid object is actually stored information, such as various types of contents in the fields defined Java source, including inherited from a parent class data content. At the same time you receive allocation strategies affect virtual machine (FieldsAllocationStyle) and fields in Java source code in order of instance data storage section.

  • Alignment padding not necessarily exist in the layout object instance, there are no particular meaning. Because the automatic memory management system requires the virtual machine size of the object need to be an integer multiple of 8 bytes. The object header size is exactly a multiple of 8 bytes (1-fold or 2-fold), when no instance data alignment, it is necessary to align the filling completion.

Examples of heap objects stored in the Java object is used to access objects, Java programs needed to operate the specific reference data object on the heap by the virtual machine on the local stack variables table. The current mainstream access method uses the handle and has a direct pointer two kinds.

At the same time, the Java heap can throw OutOfMemoryErroran exception.

Methods district

District method (Method Area) and the Java heap, as each thread is a shared memory area, which is used to store class information has been loaded in the virtual machine, constants, static variables, time compiler to compile code and other data, GC in the region appear relatively small. Meanwhile, the method also contains runtime constant pool area.

Runtime constant pool of the various symbols and literals for storing compile generated reference, this section will always enter the operating method of the amount of cell storage area after the class is loaded.

  • Various literal and conceptual level language similar to Java, contains the text string, declared as a constant final and so on;

  • Reference symbol comprising: a fully qualified class name and an interface name and descriptor and descriptors, name of the method, the field;

Meanwhile, the district method can also throw OutOfMemoryErroran exception.

summary

Above, including the JVM runtime Java .classregion loaded into the JVM divided, respectively, threads and threads share a private area. Of course, this is only a small step in-depth understanding of the process of the JVM, then also you need to know Java JVM garbage heap of markers, as well as garbage collection ......

Guess you like

Origin juejin.im/post/5e761f74e51d45270f52e22d