Java Core
    Java - Overview
    What is new in Java 21
    What is new in Java 17
    What is new in Java 11
    Java - JVM
    Java - Deprecated
    Java - Numbers
    Java - String
    Java - Time
    Java - Collections
    Java - Lambda
    Java - Exceptions
    Java - Switch
    Java - IO
    Java - Jar
    Java - Generics
    Java - Environment
    Java - Concurrency
    Java - Trouble Shooting
    Java - Install
    Java - Misc

Garbage Collection

Updated: 2021-11-19

Garbage Collection Types

  • Minor GC: or Scavenge GC. Collect garbage from the Young Generation.
  • Major GC: collect garbage from the Old Generation
  • Full GC: collects garbage from all regions

stop-the-world event

When GC runs, all application threads are paused. In Minor GCs, stop-the-world events occurs, but only momentarily.

Algorithms

  • ZGC: Z garbage collector
    • introduced in Java 11 a scalable (multi-terabyte heaps), low-latency (GC pause times <10ms) garbage collector.
  • Shenandoah: Low-Pause-Time Garbage Collector introduced in Java 12
  • G1 Garbage Collector
    • Garbage First. G1 is the replacement for the CMS GC. Since Java 7, default GC since Java 9
    • -XX:+UseG1GC
  • Epsilon Garbage Collector: A No-Op Garbage Collector, introduced in Java 11
  • Concurrent Mark Sweep (CMS) Collector,
    • deprecated in Java 9, removed in Java 14 (why: CMS is a highly configurable, sophisticated algorithm and thereby causes a lot of complexities to the GC code base in JDK.)
    • Also known as the low latency collection. This is 1 of 2 non-compacting collector.
  • Parallel Compacting Collector
    • Young and old generation are both collected in parallel and compacted.
    • -XX:+UseParallelOldGC
  • Parallel Collector
    • Collects new in parallel, but old by serial algorithm.
    • -XX:+UseParallelGC
  • Serial Collector
    • Single CPU collector for both young and old generations. Good for embedded systems without a lot of processing power. By default ergonomics does not use this unless you have less than 2 processors and less than 2GB of memory.
    • -XX:+UseSerialGC

request garbage collection

  • System.gc()

Concurrent vs. Parallel

  • Concurrent: runs along with application threads
  • Parallel: stop the world, multi-threaded

more:

  • Parallel - while the JVM is running there are application threads and garbage collector threads. A parallel phase is one carried out by multiple gc threads, i.e the work is split up between them. It says nothing about whether the gc threads might be overlapping with running application threads.
  • Serial - a phase that is serial is only carried out on a single gc thread. As with parallel above, it says nothing about whether the work overlaps with currently running application threads.
  • Stop The World - in a stop the world phase, the application threads are suspended in order for the gc to carry out its work. When you experience GC pauses this is due to a Stop The World phase.
  • Concurrent - if a phase is concurrent then the GC can carry out its work at the same time the application threads are progressing with their work. Concurrent phases are complex because they need to be able to deal with application threads potentially invalidating their work before the phase completes.
  • Incremental - if a phase is incremental then it can run for a period and terminate early due to some condition, e.g time budget or a higher priority gc phase that needs carrying out, while still having done productive work. This is in contrast to a phase that needs to fully complete for it to have been productive.