Analysis of JVM


PS: I have a superficial understanding. If there is anything wrong, I hope the boss will give me advice^^~~

Diagram of. Java file from compilation to operation

Analysis of JVM

Runtime data area (JVM)

Analysis of JVM

The runtime data is divided into two parts: thread shared area and thread exclusive area

Thread sharing area: method area (meta space), stack

Thread exclusive area: Java stack, local method stack and program counter


Method area (meta space):

1. Class file information (born during compilation): after compilation, all literal and symbolic references in all class file information will be stored in the constant pool

PS: if the defined field is not referenced elsewhere, it will not be put into the constant pool by the compiler!!!!!

2. Runtime constant pool (born when the program is running): when the program is running, all the information in the constant pool is loaded in the class loading stage. If there is data corresponding to the symbol reference in the constant pool, the symbol reference will be changed into a direct reference


Symbol reference: a symbol reference uses a group of symbols to describe the referenced target, which has nothing to do with the memory layout implemented by the virtual machine. The referenced target may not have been loaded in memory. Here, the symbolic reference can be understood as a placeholder for the real address ~.

The direct reference to the specific target in memory is a certain direct reference to the actual target in memory

Analysis of JVM


1. Store classes, methods, constants, variables, string constant pools, and referenced real objects

PS: when a string literal is used for the first time, the string literal is stored in the string constant pool as a real string object

Analysis of JVM

Java stack:

When compiling the program code, the size of the local variable table and the depth of the operand stack in the stack frame have been completely determined.

Therefore, how much memory needs to be allocated for a stack frame will not be affected by the variable data during program running, but only depends on the specific virtual machine implementation

  1. Reference of storage object and eight basic data types
  2. Each thread has a stack. A stack consists of countless stack frames, which is thread safe. Stack frames of different threads cannot refer to each other
  3. The order of stack frames is increased from bottom to top, and the principle of first in first out, last in first out is followed. The top stack frame is called the current stack frame. Only the current stack frame is valid. The method associated with this stack frame is called the current method. The process of each method from the beginning of the call to the completion of the execution corresponds to the process of a stack frame from entering the stack to leaving the stack in the JVM

Analysis of JVM

Analysis of JVM

4. A stack frame is composed of local variable table operand stack dynamic connection return address

Analysis of JVM

Native Method Stack : the main operation is called with the method modified by native dll 、. Method in so file

Program counter: if the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed. If the thread is executing a native method, this counter value is null (undefined)

Garbage collection (GC)):

Analysis of JVM


Eden Park: all objects are new in Eden Park

Survivor Zones 0 and 1 (to and from): these two zones are exchanged with each other. The one who is empty is to, and the one who is not empty is from

Old age: objects that survived from the Cenozoic

Meta space

This area is resident memory. It is used to store the class object and interface metadata of JDK. It stores some environment or class information of Java runtime ~, and there is no garbage collection in this area! Shutting down the virtual machine will free up memory in this area~

A startup class is loaded with a large number of third-party jar packages. Tomcat deploys too many applications. A large number of dynamically generated reflection classes are constantly loaded until the memory is full

Garbage collection process:

All objects are in the Eden Park. When the Eden Park is full, conduct a minorgc (light GC) to judge whether the object is alive (accessibility analysis algorithm). If the object is alive, enter the survivor area (replication algorithm is used here). When the object has experienced 15 minorgcs and still exists, it will enter the elderly generation from the survivor area (mark removal algorithm + mark compression algorithm is used here), When the old generation is full, a major GC / full GC will be performed

Minorgc (light GC)

Trigger condition: triggered when Eden area is full

Majorgc / fullgc:

Trigger condition: system Call of GC () method

Insufficient space in old age

Insufficient space in method area

After passing minor GC, the average size of entering the old age is larger than the available memory of the old age

When copying from Eden area and from space area to to space area, if the size of the object is larger than the available memory of to space, the object is transferred to the old age, and the available memory of the old age is smaller than the size of the object

Replication algorithm:

Benefit: no memory fragmentation

Disadvantages: half of the storage space is wasted. Half more space is always to (under extreme conditions)

Analysis of JVM

Mark removal algorithm:

Benefits: no waste of extra memory space

Disadvantages: after scanning twice, it takes too long and memory fragments will appear

Analysis of JVM

Tag compression algorithm:

Benefits: no waste of additional memory space, no memory fragmentation

Disadvantages: it has been scanned three times and takes too long

Analysis of JVM

Mark removal compression algorithm:

Benefit: save some time

Analysis of JVM


Memory efficiency (time utilization): copy algorithm > mark clearing algorithm > mark sorting algorithm

Memory uniformity: copy algorithm = mark compression algorithm > mark removal algorithm

Memory utilization: Mark compression algorithm > mark removal algorithm > copy algorithm

According to the advantages and disadvantages of these algorithms, it is concluded that:

It is optimal to use the generational collection algorithm (the new generation uses the replication algorithm, and the old age uses the mixed use of mark removal algorithm and mark compression algorithm)

Cenozoic: the survival rate of objects is low, so the replication algorithm is used

Old age: large area and high survival rate. Therefore, mark removal algorithm and mark compression algorithm are mixed