JVM learning (1) - JVM runtime data area

Introduction: Java is a language based on a virtual machine, so it is very important to understand and be familiar with the operating principles of virtual machines.
 
First look at a technical diagram of the Java virtual machine as a whole:


 
pile
1. Method Area, Method Area , mainly stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the virtual machine.
Also called permanent generation.
 
For example, when spring uses IOC or AOP to create beans, or uses cglib, class information is dynamically generated in the form of reflection.
If a large number of dynamic classes are generated and the heap memory is insufficient, an OutOfMemoryError exception will be thrown.
Another example is when tomcat compiles jsp into a servlet class, it will also cause this situation.
 
2. The constant pool at runtime : it is part of the method area. Used to store various literals and symbolic references generated by the compiler, which will be stored in the runtime constant pool of the method area after the class is loaded. The constant pool is dynamic and data can be added. Such as string constant pool, imtern (). In fact, it is an implementation of the Flyweight pattern.
 
Runtime constant pool overflow: For example, adding data to the constant pool will cause an OutOfMemoryError exception
 
3, java heap, java heap . It is the largest piece of memory managed by the Java virtual machine. Is a memory area shared by all threads, created when the virtual machine starts. Store object instances, arrays. The main area managed by the garbage collector is also this area, also known as the GC heap.
Divided into new generation and old generation.
Exceptions are also thrown when objects are generated in large numbers and cause insufficient memory.
 
Second, the stack
1. The virtual machine stack is private to the thread and has the same life cycle as the thread. Describes the memory model of java method execution: when each method is executed, a stack frame is created at the same time, which is used to store the local variable table (basic type, object reference), operand stack dynamic linked list, method exit and other information.
If a thread requests a stack depth greater than the virtual machine allows, a StackOverflowError exception will be thrown, such as recursive calls.
If too many threads are generated and cannot apply for enough memory, an OutOfMemoryError exception will be thrown. For example, when the number of tomcat requests is very large, set the maximum number of requests.
 
2. Native method stack, Native Method Stack . That is, calling the Native method service used by the virtual machine. The above two exceptions are also thrown.
 
3. Program calculator. Line number indicator of the bytecode executed by the current thread.
 
3. Direct memory
Not part of the virtual machine runtime data area. The nio introduced by JDK1.4 can use the Native function library to directly allocate the off-heap memory, and then operate through a DiectByteBuffer object stored in the java heap as a reference to this memory. This can improve performance in some scenarios because it avoids copying data in the Java heap and the Native heap.

 

 

4. Summary

The entire runtime data area in the virtual machine is as described above. All the operations such as allocation and recycling of java objects are in the above data area. We also write a java virtual machine according to the virtual machine specification and follow the above principles.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326201184&siteId=291194637