Interview~jvm (JVM memory structure, class loading, parent delegation mechanism, object allocation, understanding garbage collection)


1. JVM memory structure

▷ Talking about the data interaction process of various parts of the memory structure: we can also talk about life cycle and data sharing; whether it is GC or OOM

Answer: The JVM memory structure includes the program counter, virtual machine stack, local method stack, heap, and method area; it is the data area when the bytecode is running. There will be a specific allocation for the bytecode.

  • For 类信息本身, it needs to be stored in the method area ;
  • When corresponding to bytecode 类要执行, the corresponding object new 对象needs to be new in the heap space ;
  • During the corresponding execution process, there are some stack frames 方法的调用that need to be allocated on the virtual machine stack , representing the calls of methods one by one.
  • A program counter is needed throughout the process to record each corresponding step in the virtual machine stack 线程执行到哪一行.

■ Corresponding to the life cycle of each part of the JVM memory structure, the heap and method area are consistent with the JVM life cycle; while the PC register, stack, and local method stack are consistent with the thread life cycle. Therefore, the data in this heap and method area can be shared; while the pc register, stack, and local method stack are thread-private and not shared;

■ Among them, the heap and method area are subject to GC and OOM; while the pc register is not GC and does not OOM; the stack and local method stack are not subject to GC and are subject to OOM;

Simplified diagram:




2. Class loading

1. Class loading process

Answer: Class loading includes three stages: loading, linking, and initialization. Linking also includes verification, preparation, and parsing.

Loading phase: First, obtain the binary byte stream of the class through the fully qualified name of the class , and then convert the static storage structure corresponding to the byte stream into the runtime data structure of the method area ; then generate a Class object in the memory ;

Linking phase: Verification, ensuring the security of the virtual machine; preparation, allocating memory for class variables and setting default initial values; parsing, converting symbol references from the constant pool into direct references.

Initialization phase: The purpose is to execute class variables and static code blocks.



2. Class loader

Answer: Officially, class loaders are divided into two types: boot class loader and custom class loader; in detail, they include: boot class loader, extension class loader, application class loader, and user-defined class Loader.

Except for the boot class loader, other class loaders directly or indirectly inherit ClassLoader ; this boot class loader is written in C or C++ language and is used to load Java's core libraries (java, javax, sun package) ;

The inheritance relationship is that the startup class is the root parent class, the extension class inherits it, then the application class inherits the extension class loader, and the user-defined class loader inherits the application class loader.




3. Parental delegation mechanism

1. Parental delegation mechanism

Answer: jvm loads class files on demand. Only when the class is used will the class file of the class be loaded into memory to generate a Class object. Loading the class file of the class is to use the parent delegation mode, that is, the request is handed over to the parent class for processing . It is a task delegation pattern.


2. Working principle - upward delegation

  • If a class loader receives a class loading request , it will not load it first, but will delegate the request to the parent class loader for execution ;
  • If the parent class loader still has its parent class loader , it will be further delegated upward , recursively, and the request will eventually reach the top-level startup class loader ;
  • If the parent class loader can complete the class loading task, it will return successfully. If the parent class loader cannot complete the loading task, the child loader will try to load it by itself . This is the parent delegation mode.


3. Advantages of parental delegation

  • Avoid repeated loading of classes
  • Protect program security and prevent core APIs from being tampered with at will ; this protection is also a "sandbox security mechanism"
    • For example, custom class: java.lang.String is duplicated and conflicts with Java core String.




4. Object allocation

1. Object allocation process and YGC and FGC

  • Summary of survivors s0 and s1 areas: there is exchange after copying, whoever is empty will be to
  • Regarding garbage collection: Frequently collected in the new generation , rarely collected in the old generation, and almost no longer collected in the permanent generation and metaspace.
  • The purpose of using the copy algorithm in the new generation : to reduce memory fragmentation




5. Understand garbage recycling

1. Overview of garbage collection

(1) What is garbage?

  • Garbage: There are no objects pointed to by any pointers in the running program.

(2) Why is GC needed?

  • Without GC, the memory may be exhausted; without GC, the normal execution of the application cannot be guaranteed. Through gc, jvm will allocate the sorted memory to new objects.

2. Garbage collection related algorithms

  • Marking phase: Reference counting algorithm, reachability analysis algorithm ▷Identification, marking objects as dead objects (garbage)
  • Clearance phase: mark-clear algorithm, copy algorithm, mark-compression algorithm

(1) Algorithm of marking stage

■ Reference counting algorithm

  • Problem: Circular reference, causing memory leaks

可达性分析算法

  • Idea: Taking the root object collection (GC Roots) as the starting point, search whether the connected target object is reachable. If it is not reachable, it will be marked as a garbage object.

    The path traveled by this search process is called the reference chain .

What can GC Roots be? ▷Various reference objects (stack reference object, local method stack reference object, method area static property reference object, string constant pool reference object)


(2) Algorithm of clearing phase:

■ Mark-and-sweep algorithm:

  • Marking: Traversing along the root node in the GC roots collection is reachable, and the reachable mark is a reachable object. Generally, the object is marked as a reachable object in the header of the object . 【Note: 标记的是可达对象,不是垃圾对象
  • Clear: Traverse the heap memory and find that there are no objects marked as reachable in the Header and recycle them.

Advantages and Disadvantages:

  • Advantages: basic, common
  • Disadvantages: Inefficiency is not high; memory fragmentation occurs; during GC, the entire application needs to be stopped (STW), resulting in poor user experience.

■ Replication algorithm:

  • Prerequisite for use: The efficiency of the replication algorithm is based on the premise that there are few surviving objects and many garbage objects.
    • For example, the objects in the new generation are all "live and die", and the survivor area uses a copy algorithm.

Advantages and Disadvantages:

  • Advantages: There is no marking and clearing process, simple implementation, and efficient operation ; after copying, the continuity of the space is guaranteed, and there will be no "fragmentation" problem .
  • Disadvantages: Requires twice the memory space.

■ Mark-compression (collation) algorithm:

Advantages and Disadvantages:

  • Advantages: Eliminates the shortcomings of scattered memory areas in the mark-and-clear algorithm, and eliminates the high cost of halving the memory in the copy algorithm.
  • Disadvantages: In terms of efficiency, the mark-sort algorithm is lower than the copy algorithm. While moving the object, if the object is referenced by other objects, you also need to adjust the reference address.



3. Compare the efficiency and memory utilization of the three algorithms in the clearing phase:



4. Common interview questions - tell me the difference between MinorGC, MajorGC and FullGC?

  • Minor GC: GC of the new generation
  • Major GC: GC in the old age
  • Full GC: Full heap collection , garbage collection of the entire Java heap and method area

Hotspot VM's GC is divided into two types: one is partial collection (Partial GC) and the other is full heap collection (FullGC)

  • Partial collection: Garbage collection that does not completely collect the entire Java heap. Which is further divided into:

    • Young generation collection (MinorGC/YoungGC): only garbage collection of the young generation
  • Old generation collection (MajorGC/o1dGC): Just garbage collection in the old generation.

    • Mixed collection (MixedGC): Collect garbage from the entire new generation and part of the old generation.
  • Full heap collection (FullGC): Collect garbage collection of the entire java heap and method area.




If this article is helpful to you, please remember to give Yile a like, thank you!

Guess you like

Origin blog.csdn.net/weixin_45630258/article/details/126660703