Summary of very classic jvm interview questions

09b374a365f24cfea5ef686ab49cef8f.jpeg


 

 

01 Tell me about the main components of the JVM and their functions?

 

JVM includes two subsystems and two components, the two subsystems are Class loader (class loading), Execution engine (execution engine); the two components are Runtime data area (runtime data area), Native Interface (local interface). Class loader (class loading): According to the given fully qualified class name (eg: java.lang.Object) to load the class file into the methodarea in the Runtime data area. Execution engine: Executes instructions in classes. Native Interface (local interface): It interacts with native libraries and is an interface for other programming languages ​​to interact. Runtime data area (runtime data area): This is what we often call the memory of the JVM. Function: First, the Java code is converted into bytecode by the compiler, and then the class loader (ClassLoader) loads the bytecode into the memory and places it in the method area of ​​the runtime data area (Runtime data area). The bytecode file is only a set of instruction set specifications for the JVM and cannot be directly handed over to the underlying operating system for execution. Therefore, a specific command parser execution engine (Execution Engine) is required to translate the bytecode into the underlying system instructions, and then hand them over to the CPU for execution. In this process, it is necessary to call other languages. The local library interface (Native Interface) to realize the function of the whole program.

 

02 Talk about the JVM runtime data area

 

During the execution of a Java program, the Java virtual machine divides the memory area it manages into several different data areas. These areas have their own purposes, as well as the time of creation and destruction. Some areas exist with the start of the virtual machine process, and some areas are established and destroyed depending on the start and end of the thread. The memory managed by the Java virtual machine is divided into the following areas:

Program Counter Register: The line number indicator of the bytecode executed by the current thread. The job of the bytecode parser is to change the value of this counter to select the next bytecode instruction to be executed. Basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this counter to complete;

Java Virtual Machine Stacks (Java Virtual Machine Stacks): used to store information such as local variable tables, operand stacks, dynamic links, and method exits;

Native Method Stack: It has the same function as the virtual machine stack, except that the virtual machine stack serves the Java method, while the native method stack serves for the virtual machine to call the Native method;

Java heap (Java Heap): The largest piece of memory in the Java virtual machine is shared by all threads, and almost all object instances allocate memory here;

Method area (Methed Area): Used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine.

 

03 Deep copy and shallow copy

 

Shallow copy (shallowCopy) just adds a pointer to the existing memory address. Deep copy (deepCopy) adds a pointer and applies for a new memory, so that the added pointer points to the new memory. In the case of deep copy, the memory will not be released due to the error of releasing the same memory during shallow copy. Shallow copy: It only points to the copied memory address. If the original address changes, the shallow copied object will also change accordingly. Deep copy: Open up a new memory address in the computer to store the copied object.

 

04 Briefly describe the Java garbage collection mechanism

 

In java, the programmer does not need to explicitly release the memory of an object, but the virtual machine executes it by itself. In the JVM, there is a garbage collection thread, which is of low priority and will not be executed under normal circumstances. Only when the virtual machine is idle or the current heap memory is insufficient, the execution will be triggered to scan those objects that are not referenced by any and add them to the collection to be recycled for recycling.

 

05 What is GC? Why GC

 

GC means garbage collection (Gabage Collection). Memory handling is where programmers are prone to problems. Forgotten or wrong memory recovery will lead to program or system instability or even crash. The GC function provided by Java can automatically monitor whether the object exceeds the scope to achieve the purpose of automatic memory recovery. The Java language does not provide an explicit operation method to release the allocated memory.

 

06 What are the reference types in Java?

 

Strong reference: It will not be recycled when GC occurs. 

Soft references: Objects that are useful but not necessary, and will be recycled before memory overflow occurs. 

Weak references: Objects that are useful but not necessary will be recycled in the next GC.

Phantom reference (ghost reference/phantom reference): Objects cannot be obtained through phantom references. PhantomReference is used to implement phantom references. The purpose of phantom references is to return a notification during gc.

 

07 How to judge whether the object can be recycled?

 

When the garbage collector performs garbage collection, the first thing to determine is which memory needs to be reclaimed, which objects are "survival" and cannot be reclaimed; which objects are "dead" and need to be reclaimed. There are generally two ways to judge:

Reference counter method: Create a reference count for each object. When there is an object reference, the counter is +1, and when the reference is released, the count is -1. When the counter is 0, it can be recycled. It has a disadvantage that it cannot solve the problem of circular references;

Reachability analysis algorithm: starting from GC Roots to search downwards, the path traveled by the search is called the reference chain. When an object does not have any reference chain connected to GC Roots, it proves that the object can be recycled.

 

08 Tell me what garbage collection algorithms does the JVM have?

 

Mark-sweep algorithm: mark useless objects, and then clear and recycle them. Disadvantages: Inefficient, unable to remove debris. 

Copy algorithm: Divide two memory areas of equal size according to capacity, copy the living object to another block when one block is used up, and then clean up the used memory space at one time. Disadvantages: The memory usage is not high, only half of the original.

Mark-sorting algorithm: mark useless objects, let all surviving objects move to one end, and then directly clear the memory outside the end boundary.

Generation Algorithm: Divide the memory into several pieces according to the life cycle of the object, usually the new generation and the old generation. The new generation basically adopts the copy algorithm, and the old generation uses the mark sorting algorithm.

 

09 Tell me what kind of garbage collectors does the JVM have?

 

If the garbage collection algorithm is the methodology of memory recovery, then the garbage collector is the specific implementation of memory recovery. The figure below shows 7 collectors that act on different generations. The collectors used to recycle the new generation include Serial, PraNew, and Parallel Scavenge, and the collectors used to recycle the old generation include Serial Old, Parallel Old, and CMS. There is also a G1 collector used to recycle the entire Java heap. Lines between different collectors indicate that they can be used together. img Serial collector (replication algorithm): new generation single-threaded collector, marking and clearing are single-threaded, the advantage is simple and efficient; ParNew collector (replication algorithm): new generation parallel collector, actually a multi-threaded version of the Serial collector, which has better performance than Serial in a multi-core CPU environment; Parallel Scavenge collector (replication algorithm): new generation parallel collector, pursuing high throughput and efficient use of CPU. Throughput = user thread time/(user thread time + GC thread time), high throughput can efficiently use CPU time, and complete the calculation tasks of the program as soon as possible, suitable for background applications and other scenarios that do not require high interaction; Serial Old collector (mark-sorting algorithm): Old generation single-threaded collector, the old version of the Serial collector; Parallel Old collector (marking-sorting algorithm): Old generation parallel collector, throughput priority, Parallel Scavenge collector Old generation version; CMS (Concurrent Mark Sweep) collector (mark-sweep algorithm): The old generation parallel collector aims to obtain the shortest recovery pause time. It has the characteristics of high concurrency and low pause, and pursues the shortest GC recovery pause time. G1 (Garbage First) collector (mark-sort algorithm): Java heap parallel collector, G1 collector is a new collector provided by JDK1.7, G1 collector is based on the "mark-sort" algorithm, that is to say, it will not generate memory fragmentation. In addition, an important feature of the G1 collector that is different from the previous collectors is that the scope of G1 recycling is the entire Java heap (including the new generation and the old generation), while the scope of the first six collectors is limited to the new generation or the old generation.

 

10 Young Generation Garbage Collector and Old Generation Garbage Collector

 

Young generation collectors: Serial, ParNew, Parallel Scavenge Old generation collectors: Serial Old, Parallel Old, CMS whole heap collectors.

The G1 new generation garbage collector generally uses the copy algorithm. The advantage of the copy algorithm is high efficiency, but the disadvantage is low memory utilization; the old generation collector generally uses the mark-sort algorithm for garbage collection.

 

11 Describe the principle mechanism of JVM loading Class files

 

All classes in Java need to be loaded into the JVM by a class loader in order to run. The class loader itself is also a class, and its job is to read the class file from the hard disk into memory. When writing programs, we hardly need to care about class loading, because these are implicitly loaded, unless we have special usage, such as reflection, we need to explicitly load the required classes. There are two ways of class loading: 1. Implicit loading. When the program encounters an object generated by new, etc. during the running process, the class loader is implicitly called to load the corresponding class into the jvm. 2. Explicit loading. Through methods such as class. load. This is of course to save memory overhead.

 

12 What is a class loader and what are the class loaders?

 

The code block that realizes obtaining the binary byte stream of the class through the name of the class's authority is called a class loader. There are mainly four class loaders:

1. The Bootstrap ClassLoader is used to load the java core class library and cannot be directly referenced by the java program.

2. Extension class loader (extensions class loader): It is used to load the extension library of Java. Implementations of the Java Virtual Machine provide an extension library directory. The class loader looks in this directory and loads Java classes. 

3. System class loader (system class loader): It loads Java classes according to the class path (CLASSPATH) of the Java application. Generally speaking, the classes of Java applications are loaded by it. It can be obtained through ClassLoader.getSystemClassLoader(). 

4. The user-defined class loader is implemented by inheriting the java.lang.ClassLoader class.

 

13 Tell me about the execution process of class loading?

 

Class loading is divided into the following 5 steps: 

Loading: Find the corresponding class file according to the search path and import it;

Verification: Check the correctness of the loaded class file; 

Preparation: allocate memory space for static variables in the class;

Resolution: The virtual machine replaces symbolic references in the constant pool with direct references. A symbolic reference is understood as a mark, and a direct reference directly points to an address in memory; 

Initialization: Perform initialization work on static variables and static code blocks.

 

14 What is the parent delegation model?

 

Parental delegation model: If a class loader receives a class loading request, it will not load the class itself first, but will delegate the request to the parent class loader to complete. The class loader of each layer is the same, so that all loading requests will be sent to the top-level startup class loader. Only when the parent loader cannot complete the loading request (the required class is not found in its search scope), the child loader will try to load the class. When a class receives a class loading request, it will not load the class by itself first, but delegate it to the parent class, and the parent class will load it. If the parent class cannot be loaded at this time, it will feed back to the subclass, and the subclass will complete the class loading.

 

That's all for today's sharing, thank you for liking, collecting and forwarding!

Guess you like

Origin blog.csdn.net/Rocky006/article/details/131823346