This JVM knowledge includes the following parts

1. Class loading mechanism

2. JVM runtime data area

3. Memory layout of Java objects

4. JVM memory model

5. Garbage collection mechanism

Garbage collector

7. Troubleshooting

A class of loading mechanisms

The main part is this one

So how do we load it? This is our parent delegate mechanism, which is basically class, submit up, load down, source code and diagram like this, right

if(parent ! = null) { c = parent.loadClass(name,false);
  } elseC = findBootstrapClassOrNull(name); c = findBootstrapClassOrNull(name); }Copy the code

So what is the loading process, as shown here

Loading is a process in which you convert a Java file into a binary for the JVM to process.

① Get the binary byte stream of a class by its fully qualified name.

② Convert the static storage structure of this byte stream to the runtime data structure of the method area.

③ Generate a Class object for this binary stream in the heap as an access point.

What about links, validate: literal, prepare: assign defaults, parse: convert symbolic references to direct references

Run-time data area

Without further ado, look at the talisman, above

Methods area

Store: static variables, constants, just-in-time compiled class files, class information. Difference: before 1.8 it was Perm Space, now it is Meta Space

The virtual machine stack

Store: stack frame; -xss can set the stack size, default 1M; To make a stack overflow to make recursion.

Composition: local variable scale; The variables defined in the method and the parameters of the method,

Operand stack; The place where data is stored,

Dynamic link; And what this does is it knows who called it, like polymorphisms in Java, and then it knows what class it is, and the method returns the address; The literal meaning

The heap

Store: objects and arrays

Now let’s look at the pointer problem

Stack to heap; Object obj = new Object(); Method area points to heap; Private static Object obj = new Object(); Heap pointing method area; Because of the class information stored in the method area, the recognition of polymorphism means that there is a pointer to the method area in the heap.

Java object memory layout

Four JVM memory model

The memory model can be thought of as the landing of run-time data areas, so when an object comes in, how do you allocate memory space

First put it into Eden area, see if it is enough, not enough, minorGC, then try Survivor is enough, if not enough; Full GC (minorGC+MajorGC); If not, OOM. So what are the conditions for an object to enter the old age

Cenozoic age > 15; Large object (- XX: PretenureSizeThreshold configure this, greater than the number of the became a large object) dynamic age: That is, if there are more than half of the same age in the survivor area, then the objects older than or equal to this age are directly entered into the old minorGC, and the new generation can’t fit in. Then, how to retrieve such objects

Garbage recovery mechanism

What is garbage, and how do you determine it

Reference counting method

Nothing pointing to is garbage, but it doesn’t solve the problem of circular references

Accessibility analysis

Start with GCRoot (static members, threads, variables in the virtual stack, variables in the local method stack, classloaders, constants) as the head, follow the path down, can touch is good, can not touch the garbage.

Now that the garbage has been identified, how can it be recycled

Four garbage collection algorithms

Mark – clear: to clear the mark, the disadvantage is that memory is not continuous, easy to generate memory fragmentation; Copy: memory is divided into two parts, copy one end to the other end, to solve the memory discontinuity, the disadvantage is that the effective memory area is only half; Mark-collation: The garbage is collected and compressed to solve the problem of only half of the effective memory; Generation algorithm: I think this belongs to an idea, namely a summary of the first three; The old generation uses tag clearing, tag sorting, and the new generation uses copy algorithm. So how to implement the algorithm? This is about garbage collector

Garbage collector

You can see the maximum 10ms pause from Serial to G1 and ZGC, and you can see that Java is always looking for the shortest pause time, which is the direction of optimization.

Parallel collection: Collection by multiple threads

Concurrent collection: Runs with the user thread

What are the differences between CMS and G1?

CMS, the four steps are initial mark – concurrent mark – re-mark – concurrent cleanup

G1, four steps, initial tag – concurrent tag – Final tag – Filter collection (Sort the collection value of each Region and make a collection plan according to the expected GC pause time of the user)

G1 can set the pause time (-xx :MaxGCPauseMillis=20) because of its Region, which can be understood as a wall divided into multiple bricks. The collection of some bricks is called the old era, and some bricks are called the new generation.

These are known, so how to troubleshoot the error

7 Troubleshooting

1. The frequent FullGC

There are several reasons for frequent FullGC

System.gc() jmap -dump:format=b,fifile=heap.hprof PID

There are not enough steps in the old days

-xx :+HeapDumpBeforFullGC -xx :+HeapDumpAfterFullGC -xx :+HeapDumpPath= a.Perof Logs before and after FullGC printing -xx :+HeapDumpAfterFullGC -xx :+HeapDumpPath= A.Perof Use MAT to analyze the heap usage. And the new class. 2. Check whether the online CPU load is too high

Printf “%x\n” tid jstack PID > d.txt Open d.txt, run the TOP command to query the thread with the highest CPU usage. Query the hexadecimal TID to find 3. Throughput tuning

Log -xx :+PrintGCDetails -xloggc: gC. log uses the gcViewer to analyze the log, adjust the stack size, pause time and other parameters as required, and see how the gcViewer analyzes the data. 4. Deadlock check

Click the Dump button next to the Dump file to go to the Dump file. Scroll down to see which line of the file is prompted to have a deadlock, and then locate the code