Reference types

  1. Strong reference: It is not reclaimed when GC occurs

  2. Soft references: Useful but not necessary objects that are reclaimed before a memory overflow occurs

  3. Weak references: Objects that are useful but not necessary and will be collected in the next GC

  4. Virtual references (ghost references/Phantom references) : Objects cannot be obtained by virtual references

    PhantomReference is used to implement a virtual reference. The purpose of a virtual reference is to return a notification during GC

Waste identification method

  1. Reference counter
    • Create a reference count for each object, counting +1 when there is an object reference and -1 when the reference is released
    • The counter can be reclaimed when it is 0. The downside is that it does not solve the problem of circular references
  2. Accessibility analysis
    • Starting with GC Roots and searching down, the path taken by the search is called the reference chain
    • An object is proved to be recyclable when there is no chain of references to the GC Roots

GC Roots, the collection of GC Roots, is a set of references that must be active

Objects that can be used as GC Roots are:

  1. Objects referenced in the virtual machine stack (local variable table in the stack frame)
  2. An object referenced by a class static property in the method area
  3. Object referenced by a constant in the method area
  4. Objects referenced in JNI (commonly known as native methods) in the native method stack

Garbage collection algorithm

  1. Reference Counting

    • The principle is that this object has a reference, that is, increasing a count, deleting a reference to decrease a count
    • During garbage collection, only objects with a zero count are collected
    • Disadvantages: Unable to handle circular references
  2. Mark-sweep

    • The first phase marks all referenced objects, starting with the reference root node
    • The second phase goes through the heap and clears the unmarked objects
    • Disadvantages: This algorithm requires the entire application to be paused, and at the same time, memory fragmentation occurs
  3. Copying

    • Divide the memory space into two equal regions, using only one region at a time
    • During garbage collection, the current area of use is traversed and the objects in use are copied to another area. Only the objects in use are processed at a time
    • Because the replication cost is relatively small, at the same time replication in the past after the corresponding memory can be collated, will not appear “debris” problem
    • Disadvantages: Requires twice the memory space
  4. Mark-compact

    • The first phase marks all referenced objects starting with the reference root node
    • The second phase traverses the heap, moving all living objects to one end, and then clears memory directly beyond the end boundary
    • This algorithm avoids the “mark-clean” fragmentation problem, but also avoids the “copy” algorithm space problem
  5. Generational Collecting

    • Based on the analysis of the object life cycle garbage collection algorithm
    • Divide the objects in the heap into young, old, and persistent (JDK8 does not exist)
    • The generation is determined by the number of TIMES the object has undergone GC
    • Different algorithms are used for recycling for different generations
    • Today’s garbage collectors generally use this algorithm, which is a combination of several basic collection algorithms

Generational collection algorithm

Origin: Studies have found that most Java objects only survive for a short period of time, while the few that survive survive for a long time

In simple terms, the heap is divided into two parts, the young generation is used to hold new objects, and when the objects live long enough, they are moved to the old generation

Heap of generational

  1. Young Generation

    • Default to 1/3 of the total space (specify the ratio of young generation to old generation by -xx :NewRatio)
    • There are three zones: Eden, To Survivor, and From Survivor. The default ratio is 8:1:1 (specified by -xx :SurvivorRatio).
  2. It is Tenured Generation

    • The default is 2/3 of the total space
  3. Perm Generation (not exist after JDK8)

    • The method area is used to store static files, Java classes, methods, and so on
    • Persistent generation has no significant impact on garbage collection
    • In JDK8, the persistence generation is abandoned and the method area is implemented in metaspace, which belongs to local memory

Generational collection

  1. Young generation collector

    • Assume that most objects live for a short time, requiring frequent use of a time-consuming garbage collection algorithm

    • The new generation garbage collector generally adopts the replication algorithm, which has the advantage of high efficiency and the disadvantage of low memory utilization

    • The garbage collector is: Serial, ParNew, Parallel Scavenge

  2. Old generation collector

    • Assuming that the objects in the old age are highly likely to survive, when the old age GC actually triggers, it means that an error has been assumed or that the heap space has been exhausted, usually requiring a full heap scan and global garbage collection
    • In the old days, collectors generally used the mark-collation algorithm for garbage collection
    • The garbage collectors are Serial Old, Parallel Old, and CMS
  3. The whole heap collector

    G1: GC implementation with both throughput and pause times, default GC option after JDK 9

The recycling process

New objects are stored in the Eden partition of the young generation. When Eden space is exhausted, gc is triggered, usually using the replication algorithm

Global garbage collection is triggered when the tenure-generation space footprint reaches a certain value, typically using a tag collation algorithm

  1. Put the Eden and From Survivor objects into the To Survivor section
  2. Clear the Eden and From Survivor partitions
  3. From and To swap Pointers To ensure that To Survivor is null before the next GC
  4. After one copy, the age of an object in a Survivor partition is +1. When the age reaches 15 (default 15), the Survivor partition is upgraded to the old generation. Objects also go directly into the tenderly generation

Gc type

  1. Minor GC

    • Typically, a Minor GC is triggered when a new object is generated and Eden fails to apply for space
    • GC is performed in the young Eden region, clearing the inviable objects and moving the surviving objects into the Survivor region. Then clean up the two sections of Survivor
    • Very frequent GC, does not affect the old age
  2. Full GC

    The entire heap is collated, including Young, Tenured, and Perm. Be insane. The Full GC is slower than the Scavenge GC, so it should be reduced as much as possible. The following causes may cause Full GC:

    • Tenured is full
    • Perm field filled (before JDK8)
    • System.gc() is displayed calling
    • The domain allocation policy of the heap changes dynamically after the last GC

Garbage collector

Collector classification

  1. Serial collector

    • Using a single thread to handle all garbage collection is more efficient because there is no multithreaded interaction

    • The advantages of multiple processors are not available, so they are suitable for single-processor machines, and can also be used for multiprocessor machines with small data volumes

    • It can be opened using -xx :+UseSerialGC

  2. Parallel collector

    • Parallel garbage collection for young generations can reduce garbage collection time. Generally used on multi-threaded multiprocessor machines

      Open with -xx :+UseParallelGC

    • The parallel collector, introduced in JDK5 and enhanced in JDK6, allows for parallel collection of heap elders

      Open it using -xx :+UseParallelOldGC

    • If the tennant uses single-threaded garbage collection instead of concurrent collection, it will limit the ability to scale

  3. Concurrent collector

    • This ensures that most of the work is done concurrently (the application is not stopped) and that garbage collection is paused for only a small amount of time
    • This collector is suitable for medium – to large-scale applications with high response time requirements
    • Use -xx :+UseConcMarkSweepGC to open it

Common collector

  1. Serial: The earliest single-threaded Serial garbage collector
  2. Serial Old: An older version of the Serial garbage collector, which is also single-threaded and can be used as an alternative to the CMS garbage collector
  3. ParNew: is a multi-threaded version of Serial
  4. The Parallel:
    • Similar to the ParNew collector, it is a multithreaded collector
    • Parallel is a throughput first collector that can sacrifice waiting time in exchange for system throughput
  5. The Parallel Old:
    • It’s a Parallel old generation version
    • Parallel uses the copy algorithm and Parallel Old uses the mark-collate algorithm
  6. CMS: A collector with the goal of achieving minimum pause time, ideal for B/S systems
  7. G1: A GC implementation that takes into account throughput and pause times, and is the default GC option since JDK 9

CMS collector

  1. CMS: Concurrent Mark – Sweep

    • Sacrifice throughput to achieve minimum recovery pause time

    • Ideal for applications that require fast server response

    • Use -xx :+UseConcMarkSweepGC to specify the use of the CMS garbage collector

  2. CMS uses a mark-clear algorithm

    • A large amount of memory fragmentation is generated during GC
    • When the remaining memory cannot meet the program running requirements, a Concurrent Mode Failure occurs
    • The temporary CMS will use the Serial Old collector for garbage removal, and the performance will be degraded