1.1 Garbage collection algorithm

  • Mark – clear method: the first periodic clear method mark recovery object, and then unified clear marked object. A large number of discrete memory fragments are generated.
  • Copy method: split the memory in half, use one half at a time, and copy the surviving objects to the other.
  • Mark-collation method: mark and recycle objects periodically, then unify the surviving objects to one end, and directly empty the memory outside the boundary.
  • Generational collection: Divide the heap into young, old, and permanent generations.
    • Young generation: Only a few objects will survive. Eden: Survive =8:1, use copy or mark-purge.
    • Old age: Includes large memory objects and multiple minor GC (15 or more) survivable objects, using a mark-collation algorithm.

1.2 Garbage Collector

The figure above shows garbage collectors acting on different generations, and if there is a line between the two collectors, they can be used together. The region in which the collector is located indicates whether it belongs to a Cenozoic or an old-age collector.

Collector summary:

The collector Serial/parallel/concurrent Young generation/old generation algorithm The target Applicable scenario
Serial serial The young generation Replication algorithm Speed of response priority Client mode in a single-CPU environment
ParNew parallel The young generation Replication algorithm Speed of response priority In a multi-CPU environment, it works with the CMS in Server mode
Parallel Scavenge parallel The young generation Replication algorithm Throughput priority You don’t need to do it in the background
Serial Old serial The old s Mark-collation algorithm Speed of response priority Client mode in single CPU environment, backup scheme of CMS
ParNew Old parallel The old s Mark-collation algorithm Throughput priority Computations in the background without much interaction
CMS concurrent The old s Mark-clear algorithm Speed of response priority Java applications concentrated on Internet sites or B/S system servers
G1 concurrent Both Mark-tidy + copy algorithm Speed of response priority Server oriented applications, replacing CMS in the future

1.2.1 Minor and Full GC

  • Minor GC: Garbage collection that occurs in the young generation. Because Java objects are mostly ephemeral, Minor GC is very frequent and generally fast.
  • Full (Main) GC: AN older GC in which Major GC occurs, often accompanied by at least one Minor GC (although not always, the Parallel Exploiter collects the Major GC directly as part of the collector’s collection strategy). Major GC is typically 10 times slower than Minor GC.

1.2.2 Young generation collector

  1. Serial collector

usingA new generation collector of replication algorithmsAnd, more importantly, this collector isThe serialFor garbage collection, all other user worker threads must be suspended until The Serial collector finishes collecting (” Stop The World “).Operation principleSerial Old collector: advantages: Simple and efficient, with no overhead of thread interaction, it is commonly used in desktop application memory management scenarios.

  1. ParNew collector

isMultithreaded version of Serial collectorIs also a new generation collector. Except for multithreading, the rules of behavior are exactly the same as Seaial. Serial Old collector:Advantages:By default, it opens the same number of collection threads as the number of cpus, is less effective than Serial collector in a single-CPU environment and has the overhead of thread interaction. As the number of cpus increases, efficiency gradually increases. In addition,The Serial collector is the only one that works with the CMS collector.

  1. Parallel avenge

Is a parallel multithreaded young-generation collector that also uses replication algorithms. The goal of the Parallel Insane is to achieve a controlled throughput. Advantages: Suitable for tasks that do not require much interaction in the background. In addition to manually entering throughput parameters, it is also possible to dynamically obtain an optimal parameter through GC adaptive tuning strategies. This is an important difference between the Parallel Scavenge collector and the ParNew collector. However, the Parallel Scavenge collector cannot be used in conjunction with the CMS collector

1.2.3 Old age collector

  1. Serial Old collector

A single-threaded old-age collector using a mark-tidy algorithm. Used previously with the Parallel Avenge collector. Later used as a fallback for CMS in the event of concurrent collection failures.

  1. Parallel Old collector

The Parallel Exploiter is an older version of the Parallel Exploiter, usedmultithreadingAnd”Mark-tidy“Algorithm. The Younger generation use the Parallel Avenge collector.The Advantage: Parallel insane and Parallel Old collector, implement the insane and CPU resource sensitive applications.

  1. CMS (Concurrent Mark Sweep) collector

The “mark-sweep algorithm” is used to obtain the collector whose goal is the shortest collection pause time. Collection Principle:

  1. Initial tagging: Simply tagging objects that GC Roots can be directly associated with, which is fast and requires “Stop The World”.
  2. Concurrent marking: The process of GC Roots Tracing, which takes the longest.
  3. Re-mark: To correct The marked object, this phase also requires “Stop The World”.
  4. Concurrent cleanup: Concurrent cleanup requires reclaiming objects.

Advantages: The main advantages are concurrent collection and low pauses. With high response speed. Disadvantages: 1. Sensitive to CPU resources. 2. It cannot be handledFloating garbage: Because new garbage is present in the CMS concurrent cleanup phase, CMS cannot dispose of it in this collection and will have to leave it for the next GC. This part of rubbish is called “floating rubbish”. 3.Mark – clear calculationMethod to generate discontinuous memory fragmentation.

1.2.4 G1 collector

usingMark-tidy + copy algorithmTo manage both the young and the old.Consolidated heap memoryWill:The entire Java heap is divided into independent regions of equal sizeAlthough the concept of Cenozoic and old-age is still retained, the Cenozoic and old-age are no longer physically separated, but are collections of parts of regions (which need not be continuous). Predictable time model: Maintain one in the backgroundPriority listAccording to the allowable collection time,Preferentially reclaim the Region with the highest value(That’s where the name garbage-first comes from),Purposefully avoid region-wide garbage collection across the entire Java heap. The G1 collector is guaranteed to achieve the highest possible collection efficiency in a limited time.Avoid full heap scan: Maintains a corresponding Region for each Region in G1Remembered Set. When writing data of the Reference type, update the corresponding object to its Remembered Set. When collecting memory, adding Remembered Set to the enumeration scope of the GC root ensures that no full heap scans will be missed. 1. Initial mark: mark the objects that GC Roots can be directly associated with, and modify the order of the maximum value in the reclaim table. 2. Concurrent marking: GC Root begins to analyze the reachability of objects in the heap in parallel with the user program. 3. Final flag: Merge data from the Remembered Set Logs into the Remembered Set. This phase requires stopping the thread but can be performed in parallel. 4. Filter collection: First sort the collection value and cost of each Region, and make a collection plan according to the expected GC pause time of users. This phase can also be executed concurrently with the user program. However, because only a portion of regions are reclaimed, the time is controlled by the user.

1.3 Java Object survival Analysis

  1. Reference counting method

Maintains a counter counter at the object header, which is reclaimed when the counter is 0. The biggest problem with reference counting is that when two objects are referenced in a loop, the GC cannot reclaim them.

  1. Accessibility analysis

Starting from these nodes, the search goes down a path called the reference chain, using a series of objects called GC Roots as the starting point. When an object is not associated with a reference chain, it can be reclaimed. Objects that can be used as GC Roots: 1) objects referenced in virtual machine stack (local variable table in frame) 2) objects referenced by class static attributes in method area 3) objects referenced by constants in method area 4) objects referenced by JNI (Native method) in local method stack