JVM memory model and object initialization process in memory

JVM memory model

Java virtual machine managed area of ​​memory, also referred to as runtime data area, divided into several runtime data area shown in FIG.
0ba9ef09351b7863b922978619e8ae6d.png

Program Counter: the current bytecode programs executed by the line number indicator

Program counter (Program Counter Register) is a small memory space, the line number can be seen as an indicator of the current thread bytecode executed. In the conceptual model of virtual machines, the bytecode interpreter when work is through changing the value of the counter to select the next bytecode instruction to be executed , a branch, looping, branching, exception handling, and other basic functions thread recovery We need to rely on the counter to complete.

Multi-threaded Java Virtual Machine that is by turns switching and processor execution time allocating threads ways to achieve, at any time, a processor (for multi-core processors is a nucleus) will only execute a program instruction. Therefore, in order to restore to normal after switching position, each thread should have a separate program counter, complementary influence between each thread counter, separate storage, such areas as so called "thread private" memory .

If the thread is executing a Java method, the counter recording is being executed is the address of the virtual machine bytecode instructions ; Native method is being performed if this counter value is null (Undefined) . This memory area is the only one not provide for any situation in the region OutOfMemoryError Java Virtual Machine Specification.

Java virtual machine stack: memory model Java method executed: push and pop the stack frame

Like the program counter, Java Virtual Machine stack (Java Virtual Machine Stacks) is privately owned, its life cycle and the same thread.

Is a virtual machine stack Java memory model described method performed : Each method creates while performing a stack frame for storing local variable table, the operand stack, dynamic linking, method exports. Each method call until completion of the procedure, it corresponds to a stack frame to push the stack out of the stack in a virtual machine.

Often said heap (Heap) and stack memory (Stack) in the stack memory stack refers to a virtual machine, or a virtual machine stack local variable table section.

Local variable table stored in the compilation of the various known types of data (boolean, byte, char, short , int, float, long, double), object reference (reference type) and type returnAddress (pointing to the address of an instruction bytecode ).

Native method stacks: the implementation of the native method

Native method stacks (Native Method Stack) and the role played by the virtual machine stack is very similar to the difference between that virtual machine execution stack for the Java Virtual Machine method (ie bytecode) service , and the native method stacks are Native method used for the virtual machine services .

Java heap: objects and storage array instance

For most applications, Java heap (Java Heap) is a Java virtual machine memory management in the largest piece of memory . Java heap is shared by all threads in a memory area , in created when the virtual machine starts . The sole purpose of the Java heap is stored instance of an object , the object instances are almost all here to allocate memory. This is described in the Java Virtual Machine Specification is: all object instances and arrays to be allocated on the heap . With the development of the escaping JIT compiler analysis of the primary key technology matures, distribution on the stack, scalar replacement optimization techniques will result in some subtle changes, all objects are allocated on the heap gradually becomes less "absolutely" the .

Java heap is the main area managed by the garbage collector , it is also often referred to as GC heap (Garbage Collected Heap) .

Methods District: class information storage virtual machine load, constant, the static variables and compiled code

District method (Method Area) and the Java heap, as each thread is a shared memory area, which is used to type information storage has been loaded in the virtual machine, constants, static variables, the time compiler to compile the code and other data .

Memory recovery of this area is mainly for recycling and unloading constant pool for the type.

Runtime constant pool: zone part of the method, the compiler generates storing various reference symbols and literals

Runtime constant pool (Runtime Constant Pool) is part of the zone method. Class file versions in addition to the classes, fields, methods, and interface description information, the information is also a constant pool (by Constant Pool the Table) , for storing compile generates various literal references and symbols , this part the method of operation will enter the region after the class loading time constant storage pool.

Java virtual machine formats for each part (including the constant pool) Class files have strict rules, each byte is used to store that data must comply with the specifications will be recognized by the virtual machine, loaded and executed, but for runtime constant pool, Java virtual machine specification do not require any details of different vendors can provide according to their need to achieve in this area. (Reference symbol addition, direct reference time constant is also stored in the pool operation)

Java virtual machine runtime data for each area has its own function, there are also isolated thread threads share, the following specific functions of each Java Virtual Machine runtime data area, shared thread or threads and isolation may be thrown by the to sum up:
Snipaste_2019-08-02_13-24-50.jpg

In addition to time, java External memory may be allocated by a local direct method, this memory is also called direct memory. Direct Memory (Direct Memory Direct Memory native) portion of the data region is not running the virtual machine, nor is the Java virtual machine memory area defined in the specification. But this part of the memory and do not use frequently, may also lead to OutOfMemeoryError exception.

In JDK 1.4 newly added NIO (New Input / Output) type, based on the introduction of a channel (Channel) and a buffer (Buffer) in the I / O mode, it can use Native libraries directly outside the heap memory allocated , then by storing a stack of objects in Java DirectByteBuffer this memory as a reference to operate. This can significantly improve performance in some scenarios as to avoid the replicated data back and forth in the Java heap heap and Native.

Initialize the object in memory process

public class Person{
    String name;
    int age;
}

In the main method, objects are created, the process of allocating memory in the JVM below
Snipaste_2019-08-02_14-04-31.jpg
class information, a constant, static variables from the hard disk first class loader loads the bytecode (.class) file, the virtual machine is loaded, even if the compiler when the zone method, the method main (stack frame) into the compiled code data are stored in the java virtual machine stack, creating an object, the object reference table is stored in local variable stack frame, the specific object allocation and heap RAM.

Guess you like

Origin www.cnblogs.com/minghaiJ/p/11288349.html