[Java] Quick Start JVM


insert image description here

1. Introduction to JVM

JVM (Java Virtual Machine) is a process named Java, which is a virtual machine for executing Java programs.

JVM will apply for a large memory space from the operating system, and then divide this memory space into several small areas

Division of regions:

  1. heap
  2. method area
  3. Stack (Java virtual stack and native method stack)
  4. program counter
  5. runtime constant pool

Local variables in the code are placed on the stack, member variables are placed on the heap, static variables are placed in the method area, and the program counter stores the address of the next instruction to be executed.

When a JVM is in progress, there is only one copy of the heap and method area, and each thread has its own copy of the single stack and program counter.

Supplement: The concept of the method area is called before jdk1.8, and after jdk1.8 there is no method area, but a metadata area.

The method area is an area divided in the JVM application memory, and the metadata area is the local memory used

2. Introduction to class loading

Class loading is the process by which the JVM loads the bytecode of a class into memory, and verifies, prepares, and parses it. that is源代码(.java)文件编译成字节码文件(.class)

3. The process of class loading

The process of class loading is roughly divided into the following steps:

  1. load
  2. load
    • verify
    • Prepare
    • analyze
  3. initialization

Loading: Loading is the process of loading the bytecode file of a class into memory.

Verification: Verification is the process of ensuring that the bytecode of a class conforms to the Java Virtual Machine specification.

Preparation: Allocate memory space for static variables in the class

Parsing: Initialize string constants, replace symbolic references (placeholders) with direct references (memory addresses)

Initialization: Initialization is the process of executing the initialization code of a class.

When is class loading performed?

When a class is used, it triggers class loading (the class is not necessarily loaded when the program is started, but only when it is used for the first time), which is a bit similar to the "lazy mode"

4. Parental delegation

Parental delegation is a mechanism of the Java class loader to ensure the safety of class loading and avoid repeated loading.

Before talking about parental delegation, let's understand the class loader

There are mainly the following types of Java class loaders:

  1. Bootstrap Class Loader: It is the built-in class loader of the JVM and is responsible for loading classes in the Java standard library
  2. Extension Class Loader (Extension Class Loader): It is one of the standard class loaders provided by the Java platform, used to load the Java extension library (such as the class in the javax package) and the extension part of the Java virtual machine.
  3. Application Class Loader (Application Class Loader): Also known as the System Class Loader (System Class Loader), it is the default loader for loading application classes. The application class loader loads classes on the application class path, that is, classes written by developers and third-party class libraries.

The above three class loaders have a parent-child relationship. The startup class loader is the parent class of the extension class loader, and the extension class loader is the parent class of the application class loader.

When entering the class loading, the input content is the fully qualified class name, and the loading starts from the Application Class Loader. That is, when a class loader receives a request to load a class, it will first delegate to the parent class loader. If the parent loader cannot find the class, it will load it by itself. If the "Application Class Loader" at the bottom is not found all the time, an exception such as "class not found" will be thrown

The biggest advantage of loading in this order is that if the sub-defined class conflicts with the class in the Java standard library, the class that can be loaded into the standard library is still retained at this time

5. GC garbage collection

GC garbage collection is the process by which the Java Virtual Machine (JVM) automatically manages memory. It releases memory resources by automatically identifying and reclaiming unused objects, and provides a memory management mechanism so that developers do not need to manually release memory.

When learning C language, there is a keyword called malloc(dynamic memory application). Its memory release timing is uncertain, and it needs to be freereleased. If not free, this memory will continue until the end of the program. If you forget to release , it may cause a "memory leak"

Whereas in Java, objects become garbage when they are no longer referenced. The garbage collector is responsible for scanning the memory of the program, finding these objects that are no longer referenced, and reclaiming their memory space so that subsequent objects can be used.

The GC garbage collection mechanism can basically solve the problem of memory leaks, but GC is not perfect. GC has a STW (stop the world) problem

STW refers to the situation where the execution of the application is suspended during the garbage collection process. During STW, all application threads are suspended until garbage collection is complete.

STW issues can have an impact on application performance and response time, especially in large-memory or high-concurrency scenarios. The execution of the application will be interrupted, which may cause a long pause time, thereby affecting the real-time performance of the system and user experience.

What is the main content of GC garbage collection? The answer is the heap , and the memory reclaimed in GC is not reclaimed in units of "bytes", but in units of "objects".

How does GC judge whether an object is garbage?

Suppose there is an object, if there is no reference to point to it, it means that the object can no longer be used

Two typical methods of determining whether an object has a reference:

  1. Reference counting (not the approach taken by the JVM)
    • Advantages of reference counting: simple, easy to implement, and relatively high execution efficiency
    • The disadvantage of reference counting: the space utilization rate is low, and circular references may occur
  2. Reachability analysis (approach taken by JVM)

In Java, GC uses the root object as a starting point to find all objects directly or indirectly connected to the root object. These objects are called reachable objects, and they are still referenced objects in the program. Objects that have no reference chain to the root object are considered unreachable, and they will be marked as recyclable objects by the garbage collector.

6. JVM recycling method

There are several ways:

  1. Mark Sweep: Mark-sweep is the most basic garbage collection algorithm. It is divided into two phases: marking phase and clearing phase. During the marking phase, the garbage collector marks all reachable objects as live objects. During the cleanup phase, the garbage collector removes unmarked objects, freeing the memory space they occupy.
  2. Copy Algorithm: Divide memory into two areas and only use one of them at a time. After the object survives, it is copied into an unused area, and then all objects in the old area are cleared. The copy algorithm is suitable for scenarios where the survival rate of objects is low , because a larger memory space is required for copying.
  3. Marking: The mark-sweep algorithm combines the features of the mark-sweep and copy algorithms. It first marks all reachable objects, then moves the surviving objects to one end, and clears the unmoved memory space. The mark-sort algorithm is suitable for scenes with a high survival rate of objects.

7. Generational recycling

Although the above three methods can realize GC recycling, there will be some problems. Therefore, when using it, different strategies need to be adopted according to different scenarios. Therefore, a new garbage collection
mechanism has been introduced-"generational collection "

Generational recycling: The generational algorithm is based on the assumption of the object life cycle, and divides the memory into different generations, generally divided into the new generation and the old generation. Objects in the young generation have a short life cycle and are recycled using the copy algorithm; while objects in the old generation have a long life cycle and are recycled using the mark-sweep or mark-sort algorithm. Generational algorithms are suitable for most applications because the lifetime of objects is usually different.

Objects in the new generation have a low probability of being scanned by GC, and objects in the old generation have a low rate of being scanned by GC. The new generation will only enter the old generation if it has survived multiple GC scans and has not been recycled. Note: If the object is a particularly
large objects, will directly enter the old generation

This is the end of the article, thank you for watching!
insert image description here

Guess you like

Origin blog.csdn.net/m0_63463510/article/details/132032873