Java- Virtual Machine Learning Summary

Java file compilation, loading process

After writing the java file, jdk will compile the class file through javac, and classLaoder will load the bytecode file into jre through classpath

jvm data area

Contains stack, heap, program counter, method area, native method stack

Where are the constants and static variables in JAVA located in the JVM, and where are the objects located?

Constants and static variables are in the method area of ​​the JVM

The object is in the heap area of ​​the JVM

What are the JVM runtime data areas?

Native method stack, heap, stack, program counter, method area

Method area: The method area is also a very important area in the JVM. Like the heap, it is an area shared by threads.

In the method area, information of each class (including class name, method information, and field information), static variables, constants, and code compiled by the compiler are stored.

Stack memory: It is the running unit of the program, and the information stored in it is related to the current thread, including: local variables, program running status, method return value

Heap memory: The implementation of JAVA's reference passing is based on the heap memory. The same heap memory space can be pointed to by different stack memory, including: object reference

Program counter: It is a very small memory space, this space is mainly for a counting operation, the promotion of the object

Method stack memory: the content of the stack frame saved when making a recursive call, components: local variable table, operand stack, reference to the runtime constant of the class to which the current method belongs, and return address

Class loaders and parental delegation

In java, class loaders are divided into

Boostap ClassLoader (startup class loader):

It is part of the JVM and is responsible for loading the Java core class library (such as java.langthe class in the package), which is usually implemented in C/C++, not an ordinary Java class loader

Extension ClassLoader (extension class loader)

Also known as the system class loader, it is responsible for loading Java's extended class library and is located in java.ext.dirsthe directory specified by the system property.

App ClassLoader (application class loader)

Responsible for loading classes specified on the application classpath (Classpath), which is the default class loader used by most Java applications

Custom ClassLoader (custom class loader)

A class loader that can be custom-implemented by developers is used to load specific classes or implement specific class loading strategies.

Parental delegation mechanism :

When a class loader receives a request to load a class, it will first check whether it can be delegated to the parent class loader to load. Only when the parent class loader cannot find the loaded class, it will be handed over to the child class loader for loading.

Advantages of the parent delegation mechanism :

Isolation: Each class loader will only load its own class, avoiding conflicts between different class loaders

Security: Different class loaders assign different permissions to prevent malicious classes from being loaded

Reusability: Classes that have already been loaded will not be loaded repeatedly, which improves operating efficiency

Extensibility: By extending the class loader, the behavior of the class can be extended

The Java class loader and the parent delegation loading mechanism are important mechanisms for the Java virtual machine to ensure the security and isolation of class loading. It ensures the uniqueness and consistency of classes and provides a high degree of flexibility and extensibility for Java applications.

What are the GC roots?

native: native method stack

object: heap memory

final constant: method area

thread: active thread

Object referenced by synchrozied: heap memory

The object referenced by the virtual machine stack, in the virtual machine stack

What are the garbage collection algorithms?

mark-clean: find useful references, and mark, will clean without mark.

Mark-Copy: Find useful references, mark them, and then copy them to a new piece of memory to clean up the original memory.

Mark-organize: Move marked useful references to the same side of memory, and clean up the memory on the other side.

Reference counting and root reachability

Reference counting method: when an object is referenced, the reference count is incremented by one, when an object is destroyed or the reference becomes invalid, the reference count is decremented by one, and when the reference count is zero, it can be recycled.

The disadvantage is that it is easily affected by circular references, such as A refers to B, and B refers to A

Root reachability analysis: Starting from the root object, through the traversal of the reference relationship between objects, the mark that can be reached is reachable, the mark that cannot be reached is not reachable, and finally, the unreachable garbage objects are recycled.

The advantage is that it is not troubled by circular references and does not generate additional reference counting overhead. It is a common garbage collection strategy in modern programming languages.

Generational GC

Generational Garbage Collection (Generational Garbage Collection) is a garbage collection strategy, usually used to manage Java heap memory. It divides heap memory into different generations (Generation), each generation has different characteristics and recycling frequency. The main idea of ​​generational garbage collection is to divide objects into different generations according to their life cycle, and then adopt different recycling strategies for objects of different generations to improve the efficiency of garbage collection.

In Java, heap memory is usually divided into the following three generations:

  1. Young Generation : The Young Generation is the home of newly created objects. Most objects become unreachable soon after being created, so the young generation is garbage collected more frequently. The young generation is usually divided into three parts: the Eden area and two Survivor areas (usually called S0 and S1).

  2. Old Generation (Old Generation) : The old generation stores objects that have survived for a period of time. Objects that survive multiple garbage collections in the young generation are moved to the old generation.

  3. Permanent Generation (Permanent Generation) : The permanent generation is used to store metadata, method information, and static variables of the class, which is different from the young generation and the old generation. The permanent generation was used in Java 8 and earlier, but it was replaced by the Metaspace in Java 8.

Generational garbage collection works as follows:

  1. Newly created objects are allocated to the Eden area of ​​the young generation.

  2. When the Eden area is full, a young generation garbage collection is triggered. During the recycling process, surviving objects will be moved to the Survivor area, and the Eden area will be emptied at the same time.

  3. Garbage collection will also be performed on the Survivor area, and the surviving objects will be moved to another Survivor area. This process iterates multiple times.

  4. When an object survives multiple collections in the young generation, it is promoted to the old generation.

  5. When the space in the old generation is full, an old generation garbage collection is triggered to clean up unused objects.

The advantage of generational garbage collection is that the garbage collection frequency of the young generation is high, and short-term surviving objects can be quickly released, while the garbage collection frequency of the old generation is low, which can reduce the recycling cost of long-term surviving objects. This strategy usually improves garbage collection efficiency and system performance.

Guess you like

Origin blog.csdn.net/qq_34123324/article/details/132637697