【Java Virtual Machine】

1. Runtime data area

Thread public

  • Java heap

    • All objects are allocated memory here, which is the main area of ​​garbage collection ("GC heap"). Divided into the new generation and the old generation. The new generation can be further subdivided into eden, survivor space0 (s0 or from space) and survivor space1 (s1 or to space).
  • Method area

    • It is used to store data such as loaded class information, constants, static variables, and code compiled by the just-in-time compiler.
      The main goal of garbage collection in this area is the recovery of the constant pool and the unloading of classes, but it is generally difficult to achieve.
      The HotSpot virtual machine treats it as a permanent generation for garbage collection. But it is difficult to determine the size of the permanent generation, because it is affected by many factors, and the size of the permanent generation will change after each Full GC, so OutOfMemoryError exceptions are often thrown. In order to make it easier to manage the method area, starting from JDK 1.8, the permanent generation is removed and the method area is moved to the meta space, which is located in local memory instead of virtual machine memory.
      The method area is a JVM specification, and permanent generation and metaspace are both ways to implement it. After JDK 1.8, the original permanent generation data is divided into heap and meta space. The meta-information of the meta-space storage class, static variables and constant pools, etc. are put in the heap.

    • Runtime constant pool

      • The runtime constant pool is part of the method area.

The constant pool (literal and symbolic references generated by the compiler) in the Class file will be placed in this area after the class is loaded.
In addition to constants generated at compile time, dynamic generation is also allowed, such as intern() of the String class.

Thread private

  • Program counter

    • Record the address of the bytecode instruction of the virtual machine being executed (empty if the local method is being executed).
  • Java virtual machine stack

    • When each Java method is executed, a stack frame is created to store information such as the local variable table, operand stack, and constant pool reference. The process from method call to completion of execution corresponds to the process of pushing and popping a stack frame in the Java virtual machine stack.
  • Native method stack

    • The local method stack is similar to the Java virtual machine stack. The difference between them is that the local method stack serves the local method.

2. Garbage collection

where is it

  • Garbage collection is mainly carried out for the heap and method area. The three areas of the program counter, virtual machine stack, and local method stack are thread-private and only exist in the life cycle of the thread, and will disappear after the thread ends, so there is no need to garbage collect these three areas.

Determine whether an object can be recycled

  • 1. Reference counting algorithm

    • Add a reference counter to the object. When the object increases by a reference, the counter is increased by 1, and when the reference is invalid, the counter is decreased by 1. Objects with a reference count of 0 can be recycled.
      In the case of circular references between two objects, the reference counter will never be 0 at this time, making it impossible to recycle them. It is precisely because of the existence of circular references that the Java Virtual Machine does not use the reference counting algorithm.
    1. Reachability analysis algorithm
    • Start the search with GC Roots, the reachable objects are all alive, and the unreachable objects can be recycled.

The Java virtual machine uses this algorithm to determine whether an object can be recycled. GC Roots generally include the following:

The object referenced in the local variable table in the virtual machine stack The object referenced
in the JNI in the local method stack The object referenced by the
class static property in the
method area The object referenced by the constant in the method area

  • 3. Recycling of the method area

    • Because the method area mainly stores permanent generation objects, and the recovery rate of permanent generation objects is much lower than that of the new generation, it is not cost-effective to recover in the method area.
      Mainly the recovery of the constant pool and the unloading of the class.

There are many unloading conditions for a class, and the following three conditions need to be met, and the conditions may not necessarily be unloaded if the conditions are met:

All instances of this class have been recycled, and there is no instance of this class in the heap at this time.
The ClassLoader that loaded this class has been recycled.
The Class object corresponding to this class is not referenced anywhere, so the method of this class cannot be accessed through reflection anywhere.

  • 4.finalize()

    • Destructor similar to C++, used to close external resources. However, try-finally and other methods can be better, and the method is expensive to run, and the uncertainty is large, and the calling order of each object cannot be guaranteed, so it is best not to use it.

Reference type

  • 1. Strong references

    • Objects that are strongly referenced will not be recycled.

Use new a new object to create a strong reference.

  • 2. Soft references

    • Objects associated with soft references will only be reclaimed when there is not enough memory.

Use the SoftReference class to create soft references.

  • 3. Weak references

    • The object associated with the weak reference must be recycled, which means that it can only survive until the next garbage collection occurs.

Use the WeakReference class to create weak references

  • 4. Phantom references

    • Whether an object has a phantom reference will not affect its lifetime, and an object cannot be obtained through a phantom reference.
      The get method of phantom references always returns null.
      The only purpose of setting a phantom reference for an object is to receive a system notification when the object is recycled.

Garbage collection algorithm

  • 1. Mark-Clear

    • In the marking phase, the program will check whether each object is an active object, and if it is an active object, the program will mark the head of the object.
      In the cleanup phase, the object will be recycled and the flag bit will be cancelled.

The marking and clearing process is not very efficient;
a large number of discontinuous memory fragments will be generated, making it impossible to allocate memory for large objects.

    1. Marking-Organizing
    • Let all surviving objects move to one end, and then directly clean up the memory outside the end boundary.

Advantages:
no memory fragmentation.
Insufficiency:
a large number of objects need to be moved and the processing efficiency is relatively low.

    1. copy
    • Divide the memory into two blocks of equal size, and use only one block at a time. When this block of memory is used up, the surviving objects are copied to the other block, and then the used memory space is cleaned up again.
    1. Generational collection
    • Generally, the heap is divided into the young generation and the old generation.

New generation use: copy algorithm
Old generation use: mark-clear or mark-sort algorithm

Garbage collector

    1. Serial collector
    • Serial translates to serial, which means that it is executed in a serial manner.

It is a single-threaded collector and will only use one thread for garbage collection.

Its advantage is simple and efficient. In a single CPU environment, because there is no thread interaction overhead, it has the highest single-threaded collection efficiency.

It is the default new-generation collector in the Client scenario, because the memory is generally not very large in this scenario.

    1. ParNew collector
    • It is a multi-threaded version of the Serial collector.

It is the default new-generation collector in the Server scenario. In addition to performance reasons, it is mainly because in addition to the Serial collector, only it can be used with the CMS collector.

    1. Parallel Scavenge collector
    • Like ParNew, it is a multi-threaded collector.

The goal of other collectors is to minimize the pause time of the user thread during garbage collection, and its goal is to achieve a controllable throughput, so it is called a "throughput first" collector. The throughput here refers to the ratio of the CPU time used to run the user program to the total time.

    1. Serial Old collector
    • It is the old version of the Serial collector, and it is also used by the virtual machine in the Client scenario.
    1. Parallel Old collector
    • It is an old version of the Parallel Scavenge collector.

In situations where throughput and CPU resource sensitivity are important, Parallel Scavenge plus Parallel Old collector can be given priority.

    1. CMS collector
    • Concurrent, mark-clear algorithm, used in the old age, response speed priority
  • 7. G1 collector

    • Concurrent, used in the new generation and the old generation, marking and sorting and copying algorithms, server-oriented, you can specify that the time spent on the GC should not exceed N milliseconds.

Fourth, the class loading mechanism

Overview

  • Classes are dynamically loaded when they are used for the first time during runtime, instead of loading all classes at once. Because if it is loaded at once, it will take up a lot of memory.

Class life cycle

  • Loading (Loading) Verification (Verification) Preparation (Preparation) Resolution (Resolution) Initialization (Using) Unloading (Unloading)

Class loading process

    1. load
    • 1. Obtain the binary byte stream that defines the class through the fully qualified name of the class.
      2. Convert the static storage structure represented by the byte stream into the runtime storage structure of the method area.
      3. Generate a Class object representing this class in memory as the access entry for various data of this class in the method area.
    1. verification
    • Ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself.
    1. ready
    • Class variables are variables modified by static. The preparation phase allocates memory for class variables and sets initial values, using the memory in the method area.
    1. Parsing
    • The process of replacing the symbolic reference of the constant pool with a direct reference.

The parsing process can be started after the initialization phase in some cases, this is to support the dynamic binding of Java.

    1. initialization
    • The initialization phase really begins to execute the Java program code defined in the class. The initialization phase is the process in which the virtual machine executes the class constructor () method. In the preparation phase, the class variable has been assigned the initial value required by the system once, and in the initialization phase, the class variable and other resources are initialized according to the subjective plan made by the programmer through the program.

When to initialize the class

    1. Active reference
    • There is no mandatory restriction on when to load in the virtual machine specification, but the specification strictly stipulates that there are and only the following five situations must be initialized to the class (loading, verification, and preparation will follow)
      1. Encounter new, getstatic, putstatic 、Invokestatic these four bytecode instructions, if the class has not been initialized, its initialization must be triggered first.
      2. Reflection
      3. Trigger the initialization of the parent class first when initializing the
      subclass 4. Main class
    1. Passive reference
    • The behavior in the above five scenarios is called active reference to a class. In addition, all methods of referencing a class will not trigger initialization, which is called passive referencing.
      1. Referencing the static fields of the parent class through the subclass will not cause the subclass to be initialized.
      2. Referencing the class through the array definition will not trigger the initialization of this class. This process initializes the array class, which is a subclass that is automatically generated by the virtual machine and directly inherits from Object, which contains the properties and methods of the array.
      3. Constants will be stored in the constant pool of the calling class during the compilation phase. In essence, they are not directly referenced to the class that defines the constant, so the initialization of the class that defines the constant will not be triggered.

Classes and class loaders

  • If the two classes are equal, the classes themselves need to be equal and loaded using the same class loader. This is because each class loader has an independent class namespace.
    The equality here includes the return result of the equals() method, isAssignableFrom() method, and isInstance() method of the Class object of the class as true, and also includes the use of the instanceof keyword to determine the result of the object ownership relationship as true.

Class loader classification

  • Start the class loader

    • Bootstrap ClassLoader, implemented in C++, is a part of the virtual machine itself; this type of loader is responsible for storing in the <JRE_HOME>\lib directory or in the path specified by the -Xbootclasspath parameter
  • Extended class loader

    • This class loader is implemented by ExtClassLoader (sun.misc.Launcher$ExtClassLoader). It is responsible for loading all class libraries in <JAVA_HOME>/lib/ext or the path specified by the java.ext.dir system variable into the memory. Developers can directly use the extended class loader.
  • Application class loader

    • This class loader is implemented by AppClassLoader (sun.misc.Launcher$AppClassLoader). Since this class loader is the return value of the getSystemClassLoader() method in ClassLoader, it is generally called the system class loader. Under normal circumstances this is the default class loader in the program.

Parental Delegation Model

  • 1. Work process

    • A class loader first forwards the class loading request to the parent class loader, and only tries to load it by itself when the parent class loader cannot complete it. The parent-child relationship is generally realized through a combination relationship, rather than an inheritance relationship
    1. benefit
    • Make the Java class along with its class loader have a priority hierarchical relationship, so that the basic classes are unified.
    1. achieve
    • The operation process of the loadClass() method is as follows: first check whether the class has been loaded, and if not, let the parent class loader to load it. When the parent class loader fails to load, it throws ClassNotFoundException, and then try to load it by yourself.

Custom class loader implementation

  • 1. If you don’t want to break the parent delegation model, you only need to rewrite the findClass method.
    2. If you want to break the parent delegation model, then rewrite the entire loadClass method.

Three, memory allocation and recovery strategy

Minor GC and Full GC

  • Minor GC

    • The young generation is recycled when the Eden is insufficient, because the young generation objects have a short survival time, so the Minor GC will be executed frequently, and the execution speed will generally be faster.
  • Full GC

    • When the old generation is insufficient, the old generation and the young generation are collected. Objects in the old generation have a long survival time, so Full GC is rarely executed, and the execution speed will be much slower than that of Minor GC.

Memory allocation strategy

    1. Objects are allocated in Eden first
    • In most cases, objects are allocated on the young generation Eden. When the Eden space is insufficient, a Minor GC is initiated.
    1. Big objects go directly into the old age
    • Large objects refer to objects that require contiguous memory space. The most typical large objects are very long strings and arrays.
      -XX:PretenureSizeThreshold, objects larger than this value are allocated directly in the old generation to avoid a large amount of memory copy between Eden and Survivor.
    1. Long-lived objects enter the old age
    • Define an age counter for the object. If the object is born in Eden and survives the Minor GC, it will be moved to Survivor, the age will increase by 1 year, and it will move to the old age when it reaches a certain age.
      -XX:MaxTenuringThreshold is used to define the threshold of age.
    1. Dynamic object age determination
    • The virtual machine does not always require the age of the object to reach MaxTenuringThreshold to be promoted to the old age. If the sum of the size of all objects of the same age in Survivor is greater than half of the Survivor space, the objects whose age is greater than or equal to this age can directly enter the old age without Wait until the age required in MaxTenuringThreshold.
    1. Space allocation guarantee
    • Before the occurrence of Minor GC, the virtual machine first checks whether the maximum available continuous space in the old generation is greater than the total space of all objects in the new generation. If the conditions are true, then the Minor GC can be confirmed to be safe.
      If it is not true, the virtual machine will check whether the value of HandlePromotionFailure allows guarantee failure. If allowed, it will continue to check whether the maximum available continuous space in the old generation is greater than the average size of the objects promoted to the old generation. If it is greater, it will try to perform a Minor GC ; If it is less than, or the value of HandlePromotionFailure does not allow risk, then a Full GC must be performed.

Full GC trigger conditions

    1. Call System.gc()
    • It is only recommended that the virtual machine perform Full GC, but the virtual machine may not actually perform it. It is not recommended to use this method, but let the virtual machine manage memory.
    1. Insufficient space in the old age
    • The common scenarios of insufficient space in the old age include the large objects mentioned above directly entering the old age, and long-lived objects entering the old age.
      In order to avoid Full GC caused by the above reasons, you should try not to create too large objects and arrays. In addition, you can increase the size of the young generation through the -Xmn virtual machine parameter, so that objects are recycled in the young generation as much as possible, and do not enter the old generation.
    1. Space allocation guarantee failed
    • Minor GC using the copy algorithm requires the memory space of the old generation as a guarantee. If the guarantee fails, a Full GC will be executed. For details, please refer to Section 5 above.
    1. Insufficient space for permanent generation of JDK 1.7 and earlier
    • In JDK 1.7 and before, the method area in the HotSpot virtual machine was implemented with permanent generation, and the permanent generation stored some Class information, constants, static variables and other data.
      When there are many classes to be loaded, reflected classes, and methods to be called in the system, the permanent generation may be full, and Full GC will be executed if it is not configured to use CMS GC. If it still cannot be recycled after Full GC, then the virtual machine throws java.lang.OutOfMemoryError.
      To avoid Full GC caused by the above reasons, the method that can be used is to increase the permanent generation space or switch to CMS GC.
    1. Concurrent Mode Failure
    • In the process of executing CMS GC, there are objects to be placed in the old generation at the same time, and the space in the old generation is insufficient (maybe too much floating garbage during the GC process leads to a temporary lack of space), a Concurrent Mode Failure error will be reported and triggered Full GC.

Guess you like

Origin blog.csdn.net/dong8633950/article/details/114448549