jvm series 4--garbage collector

1. JAVA four reference types

  • strong reference

The most common thing in Java is strong reference. When an object is assigned to a reference variable, the reference variable is a strong reference. When an object is referenced by a strong reference variable, it is in a reachable state and it cannot be recycled by the garbage collection mechanism. Even if the object will never be used in the future, the JVM will not collect it. Therefore, strong references are one of the main causes of Java memory leaks.

  • soft reference

Soft references need to be implemented using the SoftReference class. For objects with only soft references, they will not be recycled when the system memory is sufficient. They will be recycled when the system memory space is insufficient. Soft references are often used in memory-sensitive programs.

  • weak quotation

Weak references need to be implemented using the WeakReference class, which has a shorter lifetime than soft references. For objects with only weak references, as long as the garbage collection mechanism is running, regardless of whether the JVM's memory space is sufficient, the object occupied will always be recycled. Memory.

  • virtual reference

Virtual reference requires the PhantomReference class to be implemented. It cannot be used alone and must be used in conjunction with the reference queue. The main function of virtual references is to track the status of objects being garbage collected.

2.GC generation collection algorithm VS partition collection algorithm

  1.    Generational Collection Algorithm
         Currently, mainstream VM garbage collection adopts the "Generational Collection" algorithm. This algorithm divides the memory into several blocks according to the different life cycles of objects, such as the new generation, old generation, and permanent generation in the JVM. generation, so that the most appropriate GC algorithm can be used according to the characteristics of each generation.

    In the new generation-copy algorithm,
                 every garbage collection can find that a large number of objects are dead and only a few survive. Therefore, using the copy algorithm only requires the cost of copying a small number of surviving objects to complete the collection.

    In the old generation-mark and collate algorithm,
                  because the object survival rate is high and there is no extra space to allocate it to guarantee it, the "mark-clean" or "
    mark There is no need to copy the memory and free it directly. Remove free memory.

  2.    Partition Collection Algorithm
    The partition algorithm divides the entire heap space into different continuous small intervals, and each small interval is used independently and recycled independently. The advantage of this is that it can control how many
    small intervals are recycled at a time. According to the target pause time, each time is reasonable Recycle several small intervals (instead of
    the entire heap) to reduce the pauses caused by a GC.

3.GC garbage collector

  • Serial garbage collector (single-threaded, copy algorithm)
    • Serial (English serial) is the most basic garbage collector, using a copy algorithm. It was once the only garbage collector in the new generation before JDK1.3.1. Serial is a single-threaded collector. It not only uses one CPU or one thread to complete garbage collection, but also must suspend all other working threads while performing garbage collection until the garbage collection is completed. Although the Serial garbage collector needs to suspend all other working threads during the garbage collection process, it is simple and efficient. For a single CPU environment, there is no overhead of thread interaction and the highest single-thread garbage collection efficiency can be obtained. Therefore, Serial garbage collector The collector is still the default new generation garbage collector when the Java virtual machine runs in Client mode.
  • ParNew garbage collector (Serial + multi-threading)
    • The ParNew garbage collector is actually a multi-threaded version of the Serial collector. It also uses a copy algorithm. In addition to using multi-threads for garbage collection, the rest of the behavior is exactly the same as the Serial collector. The ParNew garbage collector also performs the same process during garbage collection. All other worker threads are to be paused. The ParNew collector opens the same number of threads as the number of CPUs by default. You can limit the number of threads of the garbage collector through the -XX:ParallelGCThreads parameter. [Parallel: Parallel]
      Although ParNew is almost identical to the Serial collector except for multi-threading, the ParNew garbage collector is the default garbage collector for the new generation of many Java virtual machines running in Server mode.
  • Parallel Scavenge collector (multi-threaded copy algorithm, efficient)
    • The Parallel Scavenge collector is also a new generation garbage collector. It also uses a copy algorithm and is also a multi-threaded garbage collector. It focuses on the program achieving a controllable throughput (Thoughput, the time the CPU uses to run user code). /Total CPU consumption time, that is, throughput = time to run user code/(time to run user code + garbage collection time)). High throughput can make the most efficient use of CPU time and complete the computing tasks of the program as quickly as possible. It is mainly suitable for applications where Tasks that perform background operations without requiring much interaction. The adaptive adjustment strategy is also an important difference between the ParallelScavenge collector and the ParNew collector.
  • Serial Old collector (single-thread mark sorting algorithm)

     

    • Serial Old is the old generation version of the Serial garbage collector. It is also a single-threaded collector that uses the mark-collation algorithm. This collector mainly runs on the default old generation garbage collector of the Client's default Java virtual machine. In Server mode, it has two main uses:
      1. Used with the new generation Parallel Scavenge collector in versions before JDK1.5.
      2. As a backup garbage collection solution using the CMS collector in the old generation.
      The garbage collection process diagram of the new generation Serial and the old generation Serial Old:


      The working principle of the new generation Parallel Scavenge collector is similar to the ParNew collector. They are both multi-threaded collectors and use the replication algorithm. During the garbage collection process, All worker threads need to be paused.
      The garbage collection process diagram of Parallel Scavenge/ParNew in the new generation 
  • Parallel Old collector (multi-threaded mark sorting algorithm)

     

    • The Parallel Old collector is the old generation version of Parallel Scavenge. It uses a multi-threaded mark-collation algorithm and was only provided in JDK1.6. Before JDK1.6, the ParallelScavenge collector in the new generation could only be used with the Serial Old collector in the old generation. It could only ensure that the throughput of the new generation was given priority, but could not guarantee the overall throughput. Parallel Old was precisely used in the old generation. It also provides a throughput-prioritized garbage collector. If the system has relatively high throughput requirements, you can give priority to the matching strategy of the new generation Parallel Scavenge and the old generation Parallel Old collector. The running process diagram of the new generation Parallel Scavenge and the old generation Parallel Old collector:
  • CMS collector (multi-threaded mark sweep algorithm)
    Concurrent mark sweep (CMS) collector is an old generation garbage collector. Its main goal is to obtain the shortest garbage collection pause time, and other old generations use mark-complement algorithms. Differently, it uses a multi-threaded mark-sweep algorithm.
    Minimal garbage collection pause times can improve user experience for highly interactive programs.
    The working mechanism of CMS is more complex than other garbage collectors. The whole process is divided into the following four stages:
    • initial mark
      • Just marking objects that GC Roots can directly associate with is very fast and still requires pausing all worker threads.
    • concurrent marking
      • ​​​​​​​The process of GC Roots tracking works together with user threads without pausing the worker thread.
    • relabel
      • ​​​​​​​In order to correct the mark record of that part of the object that has changed due to the user program continuing to run during concurrent marking
        , it is still necessary to pause all worker threads.
    • Concurrent clear
      • ​​​​​​​Clear GC Roots unreachable objects and work with user threads without pausing the worker thread. Since during the longest concurrent marking and concurrent clearing processes, the garbage collection thread can work concurrently with the user, so in general, the CMS collector's memory recycling and the user thread are executed concurrently.
        CMS collector working process:

  • G1 Collector
    • Garbage FIRST garbage collector is the most cutting -edge result of the current development of the theory of garbage collectors. Compared with the two most prominent improvements of the G1 collector, the two most prominent improvements of the G1 collector are:
      1 . Based on mark-compact algorithm, no memory fragmentation occurs.
      2. The pause time can be controlled very precisely to achieve low-pause garbage collection without sacrificing throughput.
      The G1 collector avoids full-area garbage collection. It divides the heap memory into several independent areas of fixed size, and tracks the progress of garbage collection in these areas. At the same time, it maintains a priority list in the background. Each time, according to the allowed collection time, Prioritize recycling of areas with the most trash. The area division and priority area recycling mechanism ensure that the G1 collector can obtain the highest garbage collection efficiency in a limited time.

        

Guess you like

Origin blog.csdn.net/weixin_38340874/article/details/122083941