Java Virtual Machine learning summary (1) - JVM memory model

Foreword

Java Virtual Machine (Java Virtual Machine = JVM) memory space is divided into five parts, namely: a program counter; Java Virtual Machine stack; native method stacks; heap; method area. Java virtual machine memory allocation map:

Characteristics of the individual regions are summarized in the following table:

First, the program counter (PC Register)

What is the program counter?

The program counter is a smaller memory space, you can see it as a line number indicator byte code currently executing thread. In other words, the program counter which records the address that a bytecode instruction currently executing thread. Note: However, if the current thread is executing a native method, then the time the program counter is empty. 

The role of the program counter

1), the bytecode interpreter program counter is changed by sequentially reading instruction, in order to achieve flow control code, such as: the order of execution, selection cycles, exception handling.
2) In the case of multiple threads, the recording position of the program counter for execution of the current thread, so that when the thread is switched back to know where the thread last ran to. 

Features of the program counter

1), it is a small storage space
2), thread private. Each thread has a program counter.
3), is the only one not OutOfMemoryError memory area appears.
4), with the creation of the life cycle of threads created, with the end of the thread and death. 

Two, Java Virtual Machine stack (JVM Stack)

What is a Java Virtual Machine stack?

Java Virtual Machine stack method is to describe the process of running Java memory model. Java virtual machine creates a stack called a "stack frame" method for each region for the upcoming Java run, this area for some information about the process during operation needed to store this information include: local variables table (stored basic data type variable, a reference type variable, a variable of type returnAddress.), the operand stack, the dynamic link information for export. When a method is about to be run, the Java virtual machine stack is first created in the Java Virtual Machine stack for the method a "stack frame" stack frame contains the local variable table, the operand stack, dynamic linking, for export information. When the process needs to create a local variable during operation, the value will be stored in the local variable stack frame is a local variable table. When this method is finished, the corresponding method will pop the stack frame, and the release of memory space.

Local variable table:  storing basic data of various types known in the compiler, object reference (pointer reference, not the object itself), wherein the long and double the length of the data is 64-bit space two local variables, the remaining data type only one. Required for completion of the local variable table memory space assigned during compilation, when entering a method, this method requires much local variable allocation is fully determined in the stack frame of the stack frame during operation does not change the size of the local variable table space.

Note: It is often said, Java memory space is divided into "stack" and "heap", stored in a local variable stack, heap storage object. This sentence is not entirely correct! Here the "stack" can be understood, but where the "stack" represents a local variable table of the Java Virtual Machine stack. The real Java virtual machine stack is a stack frames, but each has its stack frame: the local variable table, the operand stack, dynamic linking, for export information. 

Java Virtual Machine stack features

1), create a local variable in the method when the table is executed, with the creation of a stack frame is created. Moreover, the local variable table size is determined at compile time down to just a good size distribution can be specified in advance at the time of creation. Further, during the process of operation of the local variable table does not change in size.
StackOverFlowError and OutOfMemoryError: 2), Java virtual machine stack will be two kinds of abnormal. a) StackOverFlowError: If the Java Virtual Machine stack memory size does not allow dynamic extension, then when the depth of the thread requested Stack Java Virtual Machine stack exceeds the current maximum depth of StackOverFlowError throw an exception. b) OutOfMemoryError: if the Java virtual machine stack memory size allows for dynamic expansion, and when the thread requests stack memory used up, no longer the dynamic expansion, this time OutOfMemoryError is thrown.
3), Java virtual machine thread stack is private, each thread has its own Java virtual machine stack, and with the creation of threads created, with the death of threads and death.
Note: similarities and differences StackOverFlowError and an OutOfMemoryError? StackOverFlowError indicates the current thread stack application exceeds the maximum stack depth pre-determined, but there may be a lot of memory space. The OutOfMemoryError refers to find the stack is already full, but also all run out of memory when applying the thread stack. 

Third, native method stacks (Native Stack)

What is a native method stacks?

Native method stack and the Java virtual machine stack to achieve similar functionality, but the local area is the memory model locally method method of operation. When the method is performed locally, the stack will also create a local stack frame method, method for storing the local variables of the local table, the operand stack, dynamic link, the information outlet. After the method finishes corresponding stack frame will pop and release the memory space. StackOverFlowError and will throw an OutOfMemoryError.

Fourth, the heap (Heap)

What is the heap?

Heap memory is used to store the object space. Almost all of the objects are stored on the heap. Heap is java virtual machine memory managed by the largest area of memory, but also by the various threads of shared memory region, the area of memory to store the object instance and an array of (but not all object instances in the heap) . -Xms by its size (minimum) and -Xmx (maximum) parameters ( maximum and minimum values to be less than. 1G ), when the former application for minimum memory, the default is the operating system's physical memory 1/64, the latter the JVM may apply maximum memory, physical memory defaults to 1/4, the default heap when the spare memory is less than 40%, JVM -Xmx increases heap memory to the specified size , can -XX: specify this MinHeapFreeRation = than the column; when spare memory heap is greater than 70%, JVM may be reduced to a heap memory size -Xms specified size , can XX: specify this column than MaxHeapFreeRation =, of course, in order to avoid frequent adjustments during operation the size of heap usually provided -Xms -Xmx value into the same. The new generation of heap memory = + + Older Generation lasting generations. When we garbage collection, we tend to heap memory and the new generation into the old generation (size ratio 1: 2), and the new generation by Eden Survivor0, Survivor1 at a ratio of three is 8: 1: 1, the new generation the recovery mechanisms using replication algorithm, in Minor GC, we have to stay a survival area for the object is located survival, regional real conduct is Eden + one of the survival zone, when our object longer than a certain age (default 15, you can by parameter setting), will put objects in the old generation, of course, large objects directly into the old generation. Older Generation collection algorithm uses tags to organize algorithm.

Heap of characteristics

1), threads share: the entire Java Virtual Machine is only a heap, all threads access the same heap. The program counter, Java virtual machine stack, is a native method stacks corresponding to a thread of.
2), created when the virtual machine is powered
3), the main site of garbage collection.
4) can be further broken down into: the new generation, the old era. It can be separated on the new generation: Eden, From Survior, To Survior . Different storage area for objects with different life cycles. Such areas may be used depending on the different garbage collection algorithms to more targeted, more effective.
5), both fixed heap size can also be extended, but the mainstream virtual machine heap size is scalable, so when the thread requests to allocate memory, but the stack is full and can no longer expand the memory is full, throw OutOfMemoryError. 

Fifth, the district method (Method Area)

What is the method area?

Java virtual machine specification defines a method area is a logical part of the stack. The method area to store class information has been loaded virtual machine, constants, static variables, the real-time compiler to compile the code and so on. District method, also known as "permanent generation", which is used to store the virtual machine to load class information, constants, static variables, each thread is a shared memory area. The default minimum of 16MB, a maximum of 64MB (64 bit JVM Since pointers expansion, default 85M), by -XX: PermSize and -XX: MaxPermSize size parameter limits zone method. It is a continuous heap space, permanent generational garbage collection and is years old (old generation) bundled together, so no matter who is full, will trigger garbage collection and permanent generation of old age. However, an obvious question is, when loaded by the JVM class information exceeds the capacity of the parameters -XX: MaxPermSize value set, the application will report OOM error. Parameters by -XX: PermSize and -XX: MaxPermSize set of runtime constant pool (Runtime Constant Pool): the method is part of the region, Class file versions in addition to the classes, fields, methods, and interface description information outer , there is a constant pool information for storing various reference symbols generated by the compiler, operating method of this part of the region after the class is loaded into the time constant pool.

Start removed permanently generations from the JDK7 (but not removed, still exists), stored in the permanent generation portion of the data has been transferred to the Java Heap or Native Heap: symbol references (Symbols) was transferred to the native heap; literal (interned strings ) was transferred to the java heap; static variable (class statics) was transferred to a class java heap. JDK8 started from the element space (Metaspace), the size of the element by the local memory space limitations, the new parameter (MaxMetaspaceSize) for limiting the local memory assigned to the class metadata size. If not specified, the element space dynamically adjusted as needed at runtime. You can view the specific article: https://blog.csdn.net/zhushuai1221/article/details/52122880 (the Java 8: From the Permanent Generation (PermGen) to the element space (Metaspace))

 Feature of the process area

1), threads share: method area is a logical part of the stack, the stack and thus, are shared by the threads. The entire virtual machine is only one method area.
2) permanent generation: the area of information methods generally require long-term presence, but it is a heap of logical partitions, hence the division method of the heap, the method area we called the old era.
3), low memory recovery efficiency: Information method area generally require long-term presence, the recovery may not work again after only a small amount of memory information. The main objective of the recovery of the memory area of the method are: the recovery of the constant pool and unloading of types.
4), Java Virtual Machine Specification requirements for the method area of more liberal. And heap, since it allows a fixed size, but also scalable to allow the size, allows garbage collection is not implemented. 

What is the runtime constant pool?

The method of three data storage areas: class information, a constant, static variables, the time compiler to compile the code. Time constant pool which constants are stored in the operation. We usually declare a constant in a class by public static final. This class is compiled Class files generated after all the information is stored in the class file in this class. When this class is loaded Java virtual machine, class file will be stored in the constant run-time constant method area of ​​the pool. And during the run, you can add a new constant to constant pool. Such as: intern String Class () method can be added to the string constants in the constant pool during operation. When some constant runtime constant pool is not referenced objects, but also not referenced variables, then we need to recycle garbage collector. 

Sixth, the direct memory (Direct Memory)

Direct memory and not part of the virtual machine memory , nor memory region Java Virtual Machine Specification defined. jdk1.4 the NIO newly added, and the introduction of the IO buffer mode channel, it can directly call the method allocates heap Native outer memory, the external memory is native heap memory, it will not affect the size of the heap memory. Direct memory is memory in addition to the Java virtual machine, but there are likely to be using Java. IO mode based on the introduction channel and buffered in NIO. It can be allocated by a local method call direct memory other than the Java virtual machine, and then by a direct operation of the memory storing the object in the Java heap DirectByteBuffer, without copying the data out of memory to the stack and then the operation, so as to enhance the efficiency of data operations. Direct memory size from Java virtual machine control, but since it is a memory, when memory throws OOM exception. 

Seven summary

1), the Java virtual machine memory model in a total of two "stacks", are: Java Virtual Machine stack and native method stacks. Two "stacks" feature similar to the memory model of the process is run method. And two "stacks" the same internal structure, are thread private. But the Java virtual machine is stack memory model describing the process of running a Java method, the method and the local stack memory model to describe the process of Java native methods run.
2), the Java virtual machine memory model in a total of two "stacks", is originally a heap, a method area. Se region belongs to a logical part of the stack. Heap to store objects, methods, class information storage area, constants, static variables, time compiler to compile code.
3), the Java Virtual Machine heap is the largest area of memory, is the main work area of the garbage collector.
4), the program counter, Java virtual machine stack, native method stacks are thread private, that each thread has its own program counter, Java virtual machine stack, local area method. And their life cycle and belongs to the same thread. The heap, the method area is shared by the threads, only a heap in a Java virtual machine, a method stack. And is created when the JVM startup, JVM stop just destroyed.

Guess you like

Origin blog.csdn.net/u012562943/article/details/93845731