Share the analysis of 10 key knowledge points we absolutely must master about learning JVM

Preface

  • jvm structure system

Every Java program is inseparable from the Java virtual machine, and the running of the Java program relies on a specific Java virtual machine instance. In the Java virtual machine specification, these terms are used to describe the subsystem, memory area, data type, and instruction. These components together show an abstract architecture inside an abstract virtual machine.
The Java virtual machine is mainly divided into five modules: class loader subsystem, runtime data area, execution engine, local method interface, and garbage collection module. Among them, the garbage collection module does not require the Java virtual machine garbage collection in the Java virtual machine specification, but before the infinite memory is invented, most JVM implementations have garbage collection. The runtime data area will exist in every JAVA virtual machine instance in some form, but the description of it in the Java virtual machine specification is quite abstract. The details of these runtime data structures are mostly determined by the designer of the specific implementation.
This article is limited in space, only a part of it is cut from my notes to share. If you find it useful and need more knowledge about JVM and other real interview questions (with detailed analysis and answers to the question)
, please click here : qf

Insert picture description here

1.Under what circumstances will stack memory overflow occur?

(1) Thinking

Describe the stack definition, then describe why it overflows, and then explain the relevant configuration parameters. If OK, you can write a stack overflow demo to the interviewer.

(2) My answer

The stack is private to the thread, and its life cycle is the same as that of the thread. When each method is executed, a stack frame is created to store the local variable table, operand stack, dynamic link, and method exit lamp information. The local variable table also contains basic data types, object reference types

If the stack depth requested by the thread is greater than the maximum depth allowed by the virtual machine, a StackOverflowError exception will be thrown, and the method recursive call produces this result.

Unable to apply for enough memory to complete the expansion, or there is not enough memory to create the corresponding virtual machine stack when creating a new thread, the java virtual machine will throw an OutOfMemoryError exception. (Too many threads started)

Parameter -Xss to adjust the size of the JVM stack

2. Detailed JVM memory model

(1) Thinking

Draw the JVM memory model diagram for the interviewer, and describe the definition, function, and possible problems of each module, such as stack overflow.

(2) My answer

JVM memory structure

Insert picture description here

  • Program counter: The line number indicator of the bytecode executed by the current thread, used to record the byte instruction address of the virtual machine being executed, and the thread is private.
  • Java virtual stack: store basic data types, object references, method exits, etc., thread private.
  • Native method stack: similar to virtual stack, except that it serves Native methods and is private to threads.
  • Java heap: the largest piece of java memory, all object instances and arrays are stored in the java heap, where the GC is collected, and shared by threads.
  • Method area: Store the loaded class information, constants, static variables, and code data compiled by the just-in-time compiler. (I.e. permanent belt), the main goal of recycling is the recycling of constant pools and the unloading of types, shared by each thread

3. Why should JVM memory be divided into new generation, old generation and persistent generation? Why is the new generation divided into Eden and Survivor?

(1) Thinking

Let me talk about the JAVA heap, the division of the new generation, and then talk about the conversion between them, the configuration of some parameters between them (such as: -XX:NewRatio, -XX:SurvivorRatio, etc.), and then explain why it is divided in this way. It's better to add a little understanding.

(2) My answer

1) Shared memory area division

  • Shared memory area = persistent zone + heap
  • Persistent zone = method area + other
  • Java heap = old generation + new generation
  • Young generation = Eden + S0 + S1

2) Configuration of some parameters

  • By default, the ratio of the young generation (Young) to the old generation (Old) is 1:2, which can be configured through the parameter -XX:NewRatio.
  • By default, Edem: from: to = 8: 1: 1 (can be set by parameter -XX:SurvivorRatio)

The number of times the object in the Survivor area is copied is 15 (corresponding to the virtual machine parameter -XX:+MaxTenuringThreshold)

3) Why is it divided into Eden and Survivor? Why are there two Survivor areas?

If there is no Survivor, every time a Minor GC is performed in the Eden area, the surviving objects will be sent to the old generation. The old generation is filled up quickly, triggering Major GC. The memory space of the old generation is much larger than that of the new generation. It takes much longer to perform a Full GC than the Minor GC, so it needs to be divided into Eden and Survivor.

The significance of Survivor is to reduce the objects sent to the old generation, thereby reducing the occurrence of Full GC. Survivor's pre-screening guarantees that only objects that can survive in the young generation after 16 Minor GCs will be sent to the old generation. Years.

The biggest advantage of setting two Survivor areas is to solve the fragmentation. The newly created object in Eden undergoes a Minor GC, and the surviving object in Eden will be moved to the first survivor space S0, and Eden will be emptied; wait for Eden When the area is full, the Minor GC will be triggered again, and the surviving objects in Eden and S0 will be copied and sent to the second survivor space S1 (this process is very important, because this copy algorithm ensures that S1 from S0 and Eden Two parts of live objects occupy contiguous memory space, avoiding fragmentation)

4. What is a complete GC process in JVM? How are subjects promoted to the old age?

(1) Thinking

First describe the Java heap memory division, then explain the Minor GC, Major GC, and full GC, and describe the conversion process between them.

(2) My answer

Java heap = old generation + new generation

Young generation = Eden + S0 + S1

When the space in the Eden area is full, the Java virtual machine triggers a Minor GC to collect the new generation of garbage, and the surviving objects will be transferred to the Survivor area.

Large objects (Java objects that require a large amount of continuous memory space, such as very long strings) enter the old state directly;

If the subject was born in Eden and survived the first Minor GC, and is accommodated by Survivor, the age is set to 1, and the age is +1 after every Minor GC. If the age exceeds a certain limit (15), it will be Promoted to old age. That is, the long-term surviving object enters the old state.

The old generation is full and can't accommodate more objects. After Minor GC, Full GC is usually performed. Full GC cleans up the entire memory heap-including the young generation and the old generation.

Major GC occurs in the GC of the old age. Cleaning up the old area is often accompanied by at least one Minor GC, which is more than 10 times slower than Minor GC.

5. Do you know which kinds of garbage collectors, their advantages and disadvantages, focus on cms and G1, including principles, processes, advantages and disadvantages

(1) Thinking

Be sure to remember typical garbage collectors, especially cms and G1, their principles and differences, and the garbage collection algorithms involved.

(2) My answer

1) Several garbage collectors

  • Serial collector: A single-threaded collector. When collecting garbage, you must stop the world and use the copy algorithm.
  • ParNew collector: The multi-threaded version of the Serial collector also needs to stop the world and copy the algorithm.
  • Parallel Scavenge collector: The new generation collector, the collector of the replication algorithm, the concurrent multi-threaded collector, the goal is to achieve a controllable throughput. If the virtual machine runs for a total of 100 minutes, of which garbage takes 1 minute, the throughput is 99%.
  • Serial Old Collector: It is the old version of the Serial Collector, a single-threaded collector, which uses a tag sorting algorithm.
  • Parallel Old Collector: It is the old version of Parallel Scavenge Collector, using multi-threading, marking-sorting algorithm.
  • CMS(Concurrent Mark Sweep) Collector: It is a collector that aims to obtain the shortest recovery pause time. Mark removal algorithm, operation process: initial marking, concurrent marking, remarking, concurrent removal, the end of the collection will generate a lot of space fragments .
  • G1 Collector: Implementation of the mark sorting algorithm, the operation process mainly includes the following: initial mark, concurrent mark, final mark, screening mark. No space debris is generated, and pauses can be precisely controlled.

2) The difference between CMS collector and G1 collector

The CMS collector is the old-generation collector, and can be used with the new-generation Serial and ParNew collectors;

The collection range of the G1 collector is the old and young generations, and does not need to be used in conjunction with other collectors;

CMS collector is a collector whose goal is to minimize the pause time;

G1 collector can predict the pause time of garbage collection

The CMS collector uses the "mark-sweep" algorithm for garbage collection, which is prone to memory fragmentation

The G1 collector uses the "mark-define" algorithm to integrate space and reduce memory space fragmentation.

6. How much do you know about the relevant knowledge of the JVM memory model, such as reordering, memory barrier, happen-before, main memory, working memory

(1) Thinking

First draw a diagram of the Java memory model, combined with an example of volatile, to explain what reordering and memory barriers are. It is best to write the following demo instructions to the interviewer.

(2) My answer

1) Java memory model diagram:

Insert picture description here

The Java memory model stipulates that all variables are stored in the main memory. Each thread has its own working memory. The working memory of the thread stores a copy of the main memory of the variables used in the thread. All operations must be performed in the working memory, and the main memory cannot be read or written directly. Different threads cannot directly access the variables in each other's working memory. The transmission of variables between threads requires data synchronization between their own working memory and main memory.

2) Order reordering.

Here, first look at a piece of code

public class PossibleReordering {
    
    
          static int x = 0, y = 0 ;
          static int a = 0, b = 0 ;
          public static void sain(String{
    
    } args) throws Int erruptedException {
    
    
                    Thread one = new Thread(new Runnable() [
                               public void run() {
    
    
                                         a = 1;
                                         x = b;
                                }
]);
Thread other = new Thread(new Runnable() [
            public void run() {
    
    
                      b = 1;
                      y = a; 
            }
]);
one.start() ; other.start(),
one.join() ; oher.join();
Systen.out.println("("+x+","+y+")");
 

The result of the operation may be (1,0), (0,1) or (1,1), or it may be (0,0). Because, in actual operation, the code instructions may not be executed strictly in the order of the code statements. Most modern microprocessors will adopt out-of-order execution (OoOE or OOE) methods. When conditions permit, they will directly run subsequent instructions that are currently capable of immediate execution, avoiding fetching. Waiting caused by the data required for the next instruction 3. Through out-of-order execution technology, the processor can greatly improve execution efficiency. And this is the order rearrangement.

3) Memory barrier Memory barrier, also called memory barrier, is a CPU instruction used to control reordering and memory visibility issues under specific conditions.

  • LoadLoad barrier: For such statements Load1; LoadLoad; Load2, before the data to be read by Load2 and subsequent read operations are accessed, it is guaranteed that the data to be read by Load1 has been read.
  • StoreStore barrier: For such statements Store1; StoreStore; Store2, before Store2 and subsequent write operations are executed, it is ensured that the write operations of Store1 are visible to other processors.
  • LoadStore barrier: For such statements Load1; LoadStore; Store2, before Store2 and subsequent write operations are flushed out, it is guaranteed that the data to be read by Load1 has been read.
  • StoreLoad barrier: For such statements Store1; StoreLoad; Load2, before Load2 and all subsequent read operations are executed, it is ensured that Store1 writes are visible to all processors. Its overhead is the largest of the four barriers. In most processor implementations, this barrier is a universal barrier, serving the functions of the other three memory barriers.

4) The happen-before principle

  • Single-threaded happen-before principle: In the same thread, write the operations that follow happen-before to the previous operations. The lock's happen-before principle: the unlock operation of the same lock happens-before the lock operation of this lock.
  • Volatile's happen-before principle: write operations to a volatile variable happen-before any operation on this variable (including write operations of course).
  • The transitivity principle of happen-before: if A operation happens-before B operation, B operation happens-before C operation, then A operation happens-before C operation.
  • The happen-before principle of thread start: the start method of the same thread happen-before other methods of this thread.
  • The happen-before principle of thread interruption: The call to the thread interrupt method happen-before is the code that detects the interruption sent by the interrupted thread.
  • The happen-before principle of thread termination: All operations in the thread happen-before thread termination detection.
  • The happen-before principle of object creation: The initialization of an object is completed before its finalize method call.

7. Briefly talk about the class loader you know, can it break parental delegation, and how?

(1) Thinking

First explain what a class loader is, you can draw a picture for the interviewer, and then talk about the meaning of the existence of the class loader, talk about the parent delegation model, and finally explain how to break the parent delegation model.

(2) My answer

  1. What is a class loader?

The class loader loads the class file into the JVM memory according to the specified fully qualified name and converts it into a Class object.

  • Bootstrap ClassLoader: It is implemented by C++ language (for HotSpot), and is responsible for loading the class library stored in the <JAVA_HOME>\lib directory or the path specified by the -Xbootclasspath parameter into the memory.
  • Other class loaders: implemented by Java language, inherited from the abstract class ClassLoader.

Such as:

  • Extension ClassLoader: Responsible for loading all class libraries in the <JAVA_HOME>\lib\ext directory or the path specified by the java.ext.dirs system variable.

Application ClassLoader (Application ClassLoader). Responsible for loading the specified class library on the user classpath, we can use this class loader directly. In general, if we do not have a custom class loader, this loader is used by default.

2) Parental delegation model

The working process of the parent delegation model is that if a class loader receives a class loading request, it will not first try to load the class by itself, but will delegate the request to the parent class loader to complete. This is true for every class loader. Only when the parent loader cannot find the specified class in its search range (ie ClassNotFoundException), the child loader will try to load it by itself.

Diagram of parent delegation model:

Insert picture description here

3) Why do you need a parent delegation model?

Here, first think about it, if there is no parental delegation, can the user define a java.lang.Object with the same name, java.lang.String with the same name, and put it in the ClassPath, then between the classes The comparison result and the uniqueness of the class cannot be guaranteed, so why do you need a parental delegation model? Prevent multiple copies of the same byte in memory

4) How to break the parental delegation model?

Breaking the parent delegation mechanism requires not only inheriting the ClassLoader class, but also rewriting the loadClass and findClass methods.

8. Tell me about the main JVM parameters you know

(1) Thinking

We can talk about stack configuration related, garbage collector related, and auxiliary information related.

(2) My answer

1) Stack configuration related

java -Xmx3550m -Xms3550m -Xmn2g -Xss128k

  • XX:MaxPermSize=16m

  • XX:NewRatio=4

  • XX:SurvivorRatio=4

  • XX:MaxTenuringThreshold=0

  • Xmx3550m: The maximum heap size is 3550m.

  • Xms3550m: Set the initial heap size to 3550m.

  • Xmn2g: Set the young generation size to 2g.

  • Xss128k: The stack size of each thread is 128k.

  • XX:MaxPermSize: Set the persistent generation size to 16m

  • XX:NewRatio=4: Set the ratio of the young generation (including Eden and two Survivor areas) to the old generation (excluding the permanent generation).

  • XX:SurvivorRatio=4: Set the size ratio of the Eden area to the Survivor area in the young generation. Set to 4, the ratio of two Survivor areas to one Eden area is 2:4, and one Survivor area occupies 1/6 of the entire young generation

  • XX:MaxTenuringThreshold=0: Set the maximum age of garbage. If it is set to 0, the young generation object does not pass through the Survivor area and directly enters the old generation.

2) Garbage collector related

  • XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseConcMarkSweepGC

  • XX:CMSFullGCsBeforeCompaction=5 -XX:+UseCMSCompactAtFullCollection:

  • XX:+UseParallelGC: Select the garbage collector as the parallel collector.

  • XX:ParallelGCThreads=20: Configure the number of threads of the parallel collector

  • XX:+UseConcMarkSweepGC: Set the old generation to concurrent collection.

  • XX:CMSFullGCsBeforeCompaction: Because the concurrent collector does not compress and organize the memory space, it will produce "fragmentation" after running for a period of time, which reduces the operating efficiency. This value sets how many times to run the GC to compress and organize the memory space.

  • XX:+UseCMSCompactAtFullCollection: Turn on compression for the old generation. May affect performance, but can eliminate fragmentation

3) Related auxiliary information

  • XX:+PrintGC -XX:+PrintGCDetails

  • XX:+PrintGC Output form:

[GC 118250K->113543K(130112K), 0.0094143 secs] [Full GC 121376K->10414K(130112K), 0.0650971 secs]

  • XX:+PrintGCDetails output format:

[GC [DefNew: 8614K->781K(9088K), 0.0123035 secs] 118250K->113543K(130112K), 0.0124633 secs]

[GC [DefNew: 8614K->8614K(9088K), 0.0000665 secs][Tenured: 112761K->10414K(121024K), 0.0433488 secs] 121376K->10414K(130112K), 0.0436268 secs

9. How to print thread stack information?

(1) Thinking

You can talk about the jps, top, jstack commands, and then cooperate with the online troubleshooting to answer them.

(2) My answer

Enter jps to get the process number.

top -Hp pid Get the CPU time-consuming performance of all threads in this process

jstack pid command to view the stack status of the current java process

Or jstack -l> /tmp/output.txt to print the stack information to a txt file.

You can use fastthread stack positioning

10. What is the difference between strong reference, soft reference, weak reference and phantom reference?

(1) Thinking

Let me talk about the definition of the four types of references first, which can be combined with the code to talk about, or extended to talk about the use of weak references in ThreadLocalMap.

(2) My answer

1) Strong references

We usually new an object is a strong reference, such as Object obj = new Object(); Even in the case of insufficient memory, the JVM would rather throw an OutOfMemory error than reclaim this object.

2) Soft references

If an object has only soft references, the memory space is sufficient, the garbage collector will not reclaim it; if the memory space is insufficient, the memory of these objects will be reclaimed.

SoftReference softRef=new SoftReference(str); // soft reference

3) Weak references

Objects with weak references have a shorter life cycle. In the process of the garbage collector thread scanning the memory area under its jurisdiction, once an object with only weak references is found, its memory will be reclaimed regardless of whether the current memory space is sufficient.

String str=new String("abc");
 
WeakReference<String> abcWeakRef = new WeakReference<String>(str);
 
str=null;
 
System.gc();

4) Phantom references

If an object holds only phantom references, then it is the same as if there are no references, and may be collected by the garbage collector at any time. Phantom references are mainly used to track the activities of objects being recycled by the garbage collector.

Conclusion

A very important feature of the Java language is its independence from the platform. The use of the Java virtual machine is the key to achieving this feature. If a general high-level language is to run on different platforms, at least it needs to be compiled into different target codes. After the introduction of the Java language virtual machine, the Java language does not need to be recompiled when it runs on different platforms. Java language usage mode The Java virtual machine shields the information related to the specific platform, so that the Java language compiler only needs to generate the object code (bytecode) that runs on the Java virtual machine, and it can be used on multiple platforms without modification. run. When the Java virtual machine executes the bytecode, it interprets the bytecode into machine instructions for execution on a specific platform.
Recently, it is the best time to find a job. I have collected some interview questions from major manufacturers and the latest data this year (2020). The following are some screenshots of the data (all data have been integrated into documents, and pdf compressed and packaged) .
If you have a friend in need, you can click here to get information, code: qf

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/SpringBoot_/article/details/108752564