Background: Recently began to find a job, the need to reserve the knowledge to be summarized again! A summary of the JVM is transferred from the following connection. Combined with "in-depth understanding of Java virtual Machines", it seems to have a clearer understanding.
Reprinted from: http://blog.csdn.net/seu_calvin/article/details/51404589
0. Preface
The Java virtual machine (Java virtualmachine) implements the most important feature of the Java language: platform agnostic.
platform-agnostic principle: The compiled Java program (. class file) is executed by the JVM. The JVM masks information that is specific to the platform, allowing programs to run unmodified on multiple platforms. When a Java virtual machine executes a bytecode, it interprets the bytecode as a machine instruction execution on a specific platform. Therefore, the implementation of Java platform independence.
1. JVM Structure diagram
JVM = class loader classloader+ execution engine Executionengine + Runtime data area Runtime Datacenter
First, the Java source code file is compiled into a bytecode file by the Java compiler, and then the class loader in the JVM is loaded and then executed by the JVM execution engine . During the execution of the program, the Runtime data area (memory) in the JVM is used to store the data and related information that is needed during the execution of the program.
So the memory management we often talk about in Java is about managing this space (how to allocate and reclaim memory space).
2. ClassLoader
ClassLoader loads the class file on the hard disk into the runtime data region in the JVM, but it is not responsible for whether the class file can be executed, which is the execution engine's responsibility.
Confined to space, the structure of the ClassLoader, the mechanism of the loading class, etc., are described in the jvm--Class loader summary article.
The parent delegation model and the custom ClassLoader are described in the jvm--custom class loader article.
3. Execution engine
Function: Executes the bytecode, or executes the local method.
4. Runtime DataArea
During runtime, the JVM's memory space is partitioned and allocated in the runtime data area for storage in the following areas.
(Note:JDK1.7 has shifted the constant pool into the heap!) )
PC Counter (the PC Register)
(1) Each Java thread has a PC register that records, for example, a return to the correct execution location after the thread has been switched back.
(2) If the thread is executing a Java method, the counter records the virtual machine bytecode address being executed, such as executing the native method, and the counter value is empty.
(3) This memory area is the only area in the JVM that does not specify any outofmemoryerror conditions.
JVM Stack (Java Virtual machinestacks)
(1) The JVM stack is thread-private and has the same life cycle as the thread. and when the thread has finished running, the corresponding memory is automatically reclaimed .
(2) stack inside the elements are called stack frames, each method from the call to the end of execution, is actually corresponding to a stack frame into the stack and out of the stack .
Stack frames are used to store some data when executing methods, such as local variable tables, operand stacks (required for execution of engine calculations), method exits, and so on.
(3) This area may have two exceptions: if the thread requests a stack depth greater than the virtual machine allows, a Stackoverflowerror exception will be thrown (such as repeating a function over and over again, and eventually this exception will occur). If the JVM stack can be dynamically extended (most JVMs are possible), the OutOfMemoryError exception is thrown when the extension fails to request enough memory.
Local methods Stack (Native method Stacks)
(1) The local method stack is similar to the virtual machine stack, where the difference is that the virtual machine stack serves the Java code method, and the local method stack serves the native method .
(2) Like the JVM stack, this area also throws Stackoverflowerror and OutOfMemoryError exceptions.
Method Area
(1) The method area is shared globally , such as every thread can access a static variable of the same class. In the method area, information about the classes that have been loaded by the JVM, static variables, compiler-compiled code, and so on are stored. For example, when the program through GetName, Isinterface and other methods to obtain information, the data from the method area .
(2) Because of the use of reflection mechanism, the virtual machine is difficult to speculate which class information is no longer used, so the recovery of this area is very difficult! In addition, for this area is mainly for the constant pool recovery, it is worth noting that JDK1.7 has shifted the constant pool into the heap.
(3) Similarly, OutOfMemoryError is thrown when the method area does not meet the memory allocation requirements.
run a constant pool (runtime Constant)
(1) A fixed constant information in a class, method reference information, etc. , and its space is allocated from the method area (JDK1.7 after the heap space).
(2) class file In addition to the type of version, field, method, interface, and other descriptive information, there is a constant table for the compilation of the known constants, this part will be loaded in the class into the method area (permanent generation) storage. However, the Java language does not require constants to enter the method area constant pool only if the content of the const table of the class is pre-compiled, and the new content can be put into Chang (the most typical String.intern () method) during the run time.
(3) The OutOfMemoryError exception is thrown when the constant pool cannot be applied to memory, and it has been analyzed.
Java Heap
(1) The Java heap is the largest piece of memory managed by the JVM. It is an area of memory that is shared by all threads and created when the virtual machine is started.
(2) Almost all instance objects are stored in this area. (The JIT compiler does not seem to be the case).
(3) Java heap is the main battlefield of garbage collection management. All Java heaps can be subdivided into: the new generation and the old age. The finer points are divided into the new generation:Eden Space, Fromsurvivor space, to survivor space . A summary of the specific garbage collection mechanisms of the JVM see my other jvm--memory management and garbage collection.
(4) According to the Java Virtual Machine specification, the Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous.
A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.
5. Heap and Stack differences
This is a very common face test, mainly from the following aspects to answer.
(1) Duties
The main difference is that stack memory is used to store local variables and method invocation information .
Heap memory is used to store objects in Java . whether they are member variables, local variables, or class variables, the objects they point to are stored in heap memory .
(2) Space size
The memory of the stack is much smaller than the heap memory, and if you use recursion, then your stack will quickly fill up and generate Stackoverflowerror.
For information on how to set the size of the stack memory, you can review the jvm--memory management and garbage collection instructions.
(3) Exclusive or shared
The stack memory belongs to the thread's private memory , and each thread has a stack of memory whose stored variables can only be visible in the thread to which it belongs.
objects in the heap memory are visible to all threads and can be accessed by all threads .
(4) Exception error
A Stackoverflowerror exception is thrown if the thread requests a stack depth that is greater than the depth allowed by the virtual machine.
If the JVM stack can be dynamically extended (most JVMs are possible), the OutOfMemoryError exception is thrown when the extension fails to request enough memory.
The JVM throws Java.lang.OutOfMemoryError when heap memory does not have the space available to store the generated objects.
The above is a knowledge of the JVM architecture.
(GO) Jvm--java Virtual machine architecture