JVM Garbage Collector (3)

1. Garbage collection algorithm

        The basic principle of the garbage collection algorithm is to identify and reclaim memory objects that are no longer used in order to free memory and make space available for storing new objects. The following are several common garbage collection algorithms and their application scenarios:

  1. Mark and Sweep algorithm (Mark and Sweep):

    • Basic principle: It is divided into two stages of marking and clearing. The marking phase traverses through the root object, marking all reachable objects. The cleanup phase recycles unmarked objects and releases the memory space they occupy.
    • Application scenarios: The mark-clear algorithm is suitable for dealing with irregular memory allocation and release situations, such as situations where multiple objects refer to each other and have different life cycles. However, it may lead to memory fragmentation problems and affect memory allocation efficiency.
  2. Copy algorithm (Copying):

    • Rationale: Divide the memory space into two equal areas, and only use one of them at a time. When garbage collection is required, the surviving objects are copied from the currently used area to another area, and then all objects in the current area are cleared.
    • Application scenarios: The replication algorithm is suitable for scenarios where a large number of objects are created temporarily and have a short life cycle. It is simple and efficient, but it will waste some memory space.
  3. Mark and Compact algorithm (Mark and Compact):

    • Rationale: Similar to the mark-sweep algorithm, but after the cleanup phase, the mark-sweep algorithm moves surviving objects to one end, and then clears the memory area outside the boundary.
    • Application scenarios: The mark-sort algorithm is suitable for dealing with scenarios with irregular memory distribution, which can reduce the problem of memory fragmentation. It is suitable for long-running applications, but may cause long pause times.
  4. Generational algorithm (Generational):

    • Basic principle: According to the life cycle of the object, the memory is divided into different generations (Generation), usually divided into the Young Generation (Young Generation) and the Old Generation (Old Generation). The objects in the new generation have a short life cycle, and the copy algorithm is used for garbage collection; the objects in the old generation have a long life cycle, and the mark-sweep or mark-sort algorithm is used for garbage collection.
    • Application scenarios: The generational algorithm is suitable for most applications because it can better adapt to the life cycle of different objects. The copy algorithm of the young generation is suitable for scenarios with a large number of short-lived objects, while the mark-sweep or mark-compact algorithm of the old generation is suitable for long-lived objects.
  5. Concurrent Marking algorithm (Concurrent Marking):
    • Rationale: The concurrent marking algorithm allows the garbage collector and application threads to execute concurrently during garbage collection. It maintains marking consistency by recording changes to objects during the marking process, and by handling concurrently modified objects.
    • Application scenarios: The concurrent marking algorithm is suitable for application scenarios that have strict requirements on system pause time, such as interactive applications or real-time systems.
    • Different garbage collection algorithms can be selected according to the characteristics and needs of the application, and each algorithm has its advantages and disadvantages. Understanding the principles and applicable scenarios of these algorithms can help us choose an appropriate garbage collection strategy to improve application performance and memory utilization.

2. Garbage collector

Different garbage collectors have different characteristics and usage. The following are several common garbage collectors and their characteristics:

  1. Serial Collector:

    • Features: The serial collector uses a single thread for garbage collection, that is, only one thread is executing during garbage collection. It is suitable for single-core processors or small applications, with simple and efficient features.
    • How to use: The serial collector can be enabled by using the "-XX:+UseSerialGC" parameter on the command line.
  2. Parallel Collector:

    • Features: The parallel collector uses multiple threads to perform garbage collection tasks in parallel, which can make full use of the advantages of multi-core processors to speed up garbage collection.
    • How to use: The parallel collector can be enabled by using the "-XX:+UseParallelGC" parameter on the command line.
  3. Concurrent Collector:

    • Features: The concurrent collector allows garbage collection and application threads to execute concurrently, reducing pause times. It is suitable for scenarios with strict requirements on system pause time.
    • Usage: Common concurrent collectors include CMS (Concurrent Mark Sweep) collector and G1 (Garbage-First) collector. The CMS collector can be enabled by using the "-XX:+UseConcMarkSweepGC" parameter on the command line, and the G1 collector can be enabled with the "-XX:+UseG1GC" parameter.
  4. G1 collector (Garbage-First Collector):

    • Features: The G1 collector is a garbage collector for server applications, which has the characteristics of efficient memory recovery and low pause time. It divides heap memory into regions to enable incremental and concurrent garbage collection.
    • How to use: The G1 collector can be enabled by using the "-XX:+UseG1GC" parameter on the command line.

        The choice of how to use the garbage collector should be weighed according to the needs of the application, system configuration, and hardware environment. Generally speaking, the serial collector is suitable for small applications or test environments, the parallel collector is suitable for multi-core processors and scenarios requiring high throughput, and the concurrent collector and G1 collector are suitable for applications with strict requirements on pause time Scenes. When in use, it can be configured and tuned according to specific needs, such as adjusting the heap size, adjusting the parameters of the garbage collector, etc., to achieve better performance and response time.

3. Memory allocation method

The memory allocation strategy of an object involves two areas, the new generation and the old generation, which have different memory allocation methods.

  1. New generation memory allocation strategy:

    • Eden area: Most newly created objects are first allocated to the Eden area. When the Eden area is full, Minor GC (new generation garbage collection) is triggered to copy the surviving objects to the Survivor area.
    • Survivor area: The Survivor area is divided into two areas of equal size, generally called the From area and the To area. When a Minor GC occurs, the surviving objects will be copied from the Eden area and the previous Survivor area (which may be the From area or the To area) to another Survivor area, and the original space will be emptied.
    • Age counter: When each object undergoes a Minor GC in the Survivor area, its age will increase by 1. When the age of an object reaches a certain threshold (15 by default), it will be promoted to the old generation.
  2. Old generation memory allocation strategy:

    • Large objects directly enter the old generation: If the size of the object exceeds a certain threshold (2MB by default), it will be directly allocated in the old generation to avoid multiple copies in the new generation.
    • Object promotion: When an object survives multiple Minor GCs in the young generation and reaches the age threshold for promotion, it will be promoted to the old generation.
    • Major GC (Full GC): When there is insufficient space in the old generation, Major GC will be triggered to perform garbage collection on the entire heap, including the new generation and the old generation.

Summarize:

  • The new generation uses a copy algorithm to recycle garbage objects in the new generation through continuous Minor GC.
  • The old generation adopts the mark-sweep or mark-compact algorithm to reclaim garbage objects in the entire heap through Major GC.
  • Large objects go directly to the old generation, avoiding multiple copies in the new generation.

These memory allocation strategies are designed to optimize the object allocation and recovery process, improve the efficiency of garbage collection and memory utilization.

Guess you like

Origin blog.csdn.net/zz18532164242/article/details/130736585
Recommended