Java Interview Question Notes - JVM

1. Summary of knowledge points

JVM is the foundation of Java operation. During the interview, you will definitely encounter JVM-related problems. The content is relatively concentrated, but the depth requirements are high.
insert image description here
Among them, the memory model, class loading mechanism, and GC are the key aspects. The performance tuning part is more application-oriented. Emphasis on practical ability. Compiler optimization and execution mode are partial to theoretical foundation, focusing on mastering knowledge points.

You need to understand the role of each part of the
memory model and what data is saved.

Class loading parent delegation loading mechanism, which type of classes are loaded by common loaders.

The idea and basis of GC generational recycling and the recycling ideas and suitable scenarios of different garbage collection algorithms.

Performance tuning often has the role of JVM optimization parameters, the basis for parameter tuning, what problems can be analyzed by commonly used JVM analysis tools and how to use them.

The advantages and disadvantages of the execution mode interpretation/compilation/mixed mode, the layered compilation technology provided by Java7, the JIT just-in-time compilation technology, the replacement on the OSR stack, the scenarios for the C1/C2 compiler, and the C2 for the server mode, and the optimization is more radical. New technology Java10's graal compiler

The compiler optimizes the compilation process of javac, ast abstract syntax tree, compiler optimization and runner optimization.

2. Detailed explanation of knowledge points

2.1, JVM memory model

Thread exclusive: stack, native method stack, program counter
Thread shared: heap, method area

2.2, stack

Also known as method stack, thread private, thread execution method will create a stack array to store local variable table, operation stack, dynamic link, method exit and other information. When calling a method, it will be pushed into the stack, and the method return will be executed out of the stack. .

2.3, local method stack

Similar to the stack, it is also used to save the information of the executed method. The Java method is executed using the stack, and the native method stack is used when executing the Native method.

2.4, program counter

Saves the bytecode position executed by the current thread. Each thread has an independent counter when working, only for executing Java methods. When executing Native methods, the program counter is empty.

2.5, heap

The largest piece of JVM memory management is shared by threads for the purpose of storing object instances. Almost all object instances will be placed here. When there is no free space in the heap, an OOM exception will be thrown. Depending on the life cycle of the object, The JVM manages the objects by generation, and the garbage collector performs the garbage collection management.

2.6. Method area

Also known as the non-heap area, it is used to store data such as class information, constants, static variables, and code optimized by the real-time compiler that have been loaded by the virtual machine. The permanent generation of 1.7 and the meta space of 1.8 are an implementation of the method area

There are two main points to answer to answer these questions:

  1. Function of each part
  2. Is it thread sharing?

2.7, JMM and memory visibility

insert image description here
JMM is the access rule that defines the variables in the program. The operation of the thread on the variable can only be carried out in its own working memory, and cannot directly operate on the main memory. Due to the reordering of instructions, the order of reading and writing will be disrupted, so JMM needs to Provides atomicity, visibility, and ordering guarantees.
insert image description here

3. Class loading and unloading

loading process

insert image description here
Among them , verify, prepare, and resolve the collective link

Load the fully qualified name of the class, find such a bytecode file, and create a Class object from the bytecode file.

Verify that the Class file meets the requirements of the current virtual machine and will not endanger the security of the virtual machine itself.

Prepare for memory allocation, allocate memory for static-modified class variables, and set initial values ​​(0 or null). Final-modified static variables are not included, because final variables are allocated at compile time.

Parse the process of replacing symbolic references in the constant pool with direct references. Direct references are pointers directly to the target or relative offsets, etc.

Initialization mainly completes the execution of static blocks and the assignment of static variables. The parent class is initialized first, and then the current class is initialized. It will only be initialized when the class is actively used.

Trigger conditions include when an instance of a class is created, when a static method or variable of a class is accessed, when a class is reflected using Class.forName, or when a subclass is initialized.

The classes loaded by Java's own loader will not be unloaded during the life cycle of the virtual machine, only the classes loaded by the user-defined loader can be unloaded.

3.1. Loading Mechanism - Parent Delegation Mode

insert image description here
Parent delegation mode, that is, when a loader loads a class, it first delegates the request to its own parent class loader for execution, until the top-level startup class loader. The parent class loader can complete the loading and return successfully, and the child class loader can not only do it by itself try to load.

advantage:

  1. Avoid duplicate loading of classes
  2. Avoid tampering with Java's core API

3.2. Generational recycling

Generational collection is based on two facts: most objects will be unused soon, and some will not be useless immediately, but will not last long.
insert image description here
Young generation -> mark - copy
old generation -> mark - clear

3.3. Recycling algorithm

3.3.1, CMS algorithm

Before 1.7, the mainstream garbage collection algorithm is a mark-clear algorithm. The advantage is concurrent collection and small pause.
insert image description here
The initial mark will be StopTheWorld, and the marked object is the root, that is, the most directly reachable object.

Concurrent marking GC threads and application threads execute concurrently, marking reachable objects.

Remark the second StopTheWorld, the pause time is much smaller than the concurrent mark, but slightly longer than the initial mark. Mainly rescan and mark the object .

Concurrent Cleanup Perform concurrent garbage cleanup.

Concurrency resets the relevant data structures for the next GC reset.

3.3.2. G1 algorithm

The default garbage collection algorithm after 1.9 is characterized by maintaining a high recovery rate and reducing pauses. It uses only a part of each time to clean up, instead of cleaning up all incremental cleanups to ensure that the pause time will not be too long.
insert image description here
It cancels the young generation and the old generation. The physical division of the age, but still belongs to the generational collector, the algorithm divides the heap into several logical regions (regions), some are used for the young generation, some are used for the old generation, and there are partitions for storing huge objects.

The same as CMS, it will traverse all objects, mark the reference situation, and copy and move the area after clearing the object to integrate the fragmented space.

Young generation recycling:
Parallel replication adopts replication algorithm, parallel collection, and StopTheWorld.

Old generation recycling:
will be recycled together with the young generation

The initial mark completes the mark of the heap root object, and it will StopTheWorld.
Concurrently mark the GC thread and the application thread to execute concurrently. The
final mark completes the three-color marking cycle and will StopTheWorld.
Copy/clear will give priority to the area with increased reclaimable space.

3.3.3. ZGC algorithm

The efficient garbage collection algorithm provided in 1.11, designed for large heap memory, can handle TB-level heaps, and can achieve a recovery pause time of less than 10ms.
insert image description here

  • Shading pointer
  • read barrier
  • Concurrent processing
  • region based
  • Memory Compression (Organization)

Marking : Marking the root object, it will StopTheWorld.
Concurrent marking : Using the read barrier to run the mark with the application thread, StopTheWorld may occur.
Clearing will clean up the objects marked as unavailable.
Roots relocation : It is to move surviving objects, In order to free up a large block of memory space and reduce fragmentation. Relocation will initially StopTheWorld, but it depends on the ratio of the relocation set to the total active set of the object.
Concurrent relocation is similar to concurrent marking .

4. Summary

4.1. Inspection point

  1. Deep understanding of the JVM memory model
  2. Understand the class loading mechanism
  3. Understanding Memory Visibility
  4. Understand commonly used GC algorithm implementations and applicable scenarios
  5. Can even choose the appropriate JVM parameters and GC algorithm according to the business scenario

4.2. Extra points

  1. compiler optimization
  2. Troubleshooting experience and ideas
  3. JVM tuning experience and tuning ideas
  4. Learn about the latest technology trends (ZGC and Graalvm)

4.3. Interview questions

  1. Briefly describe the memory model of the JVM
  2. When will FullGC be triggered
  3. There are several types of Java class loaders, what is the relationship
  4. 1.8 first, replace PermGenMeatspace with Metaspace, where is it stored?
  5. What optimizations the compiler will do to the instructions (the short answer describes the compiler's instruction rearrangement)
  6. Briefly describe what problems volatile can solve and how to do it
  7. Briefly describe the generational recovery of GC
  8. The difference between G1 and CMS
  9. What are the types of object references and what are their characteristics
  10. Which JVM debugging tools have been used, and what content has been mainly analyzed

Guess you like

Origin blog.csdn.net/m0_54853503/article/details/124273002