Java GC

young generation and old generation. 1 eden and 2 survivor spaces.

minor GC, mark and copy, from eden and one survivor to the other survivor

full GC, mark, sweep and compact generations

both will stop the world.

- Serial GC, for small heap and single thread env

- Parallel GC, multiple thread to do GC, but stop application when do both minor GC and full GC. suitable for high throughput application due to the facts

  • all cores are cleaning garbage during collection
  • no cores are used for cleaning between collection cycles.

- CMS GC, low latency, but less throughput than parallel GC if it's cpu-bound application due to the fact that some CPU cores are cleaning garbage in most of time thus they are not working on application.

  • parallel mark-copy stop-the-world collection in young generation (minor GC)
  • mostly (two of phases stop the world) concurrent-mark-sweep in old generation (full GC)
  1. Initial Mark (STW), collecting all GC roots in old generation (direct GC roots or indirectly referenced from young generation)
  2. Concurrent Mark, marking all live objects (may not all of them due to application may mutate references at the same time) in old generation from the GC roots.
  3. Concurrent Preclean, accounting for references changed during previous marking phase. 
  4. Concurrent Abortable Preclean, offloading as much work as possible from the final mark phase
  5. Final remark (STW)
  6. Concurrent sweep
  7. Concurrent reset

collection in young generation may occur when concurrent collection happens in old generation.

drawbacks are fragmentation in old generation and non-predictable pause duration especially in large heap.

- G1 GC (collect regions with more garbage)

heap is divided into regions, including eden regions, survivor regions and old regions.

  • Young GC (STW, in parallel), coping to survivor regions or old regions
  • GC in old generation
  1. Initial Mark (STW), mark survivor regions (root regions), it's piggybacked on young GC
  2. Root Region Scanning, find references to old generation
  3. Concurrent Marking, find live objects over entire heap
  4. Remark (STW), complete the marking of live objects. (snapshot-at-the-beginning). empty regions are removed and reclaimed. region liveness is calculated for all regions.
  5. Cleanup/copying, selecting regions with lowest liveness to be collected at the same time as young GC.

reference: https://plumbr.io/handbook/garbage-collection-algorithms-implementations#concurrent-mark-and-sweep

http://www.oracle.com/technetwork/tutorials/tutorials-1876574.html

;