Garbage Collection Algorithms

Harshana Samarasinghe
CodeX
Published in
2 min readJul 2, 2022

Garbage Collection means Automatic memory management is accomplished by garbage collection (GC). The garbage collector makes an effort to recover memory that the application allocated but is no longer referenced — also known as garbage.

Mark — This shows which parts of the memory are being utilized and which parts are being left unused.

Sweep: Removes the items that were marked in the Mark phase.

There are mainly 5 types of GC implementations.

  • Serial Garbage Collector

The garbage collection process is done here using a single thread. This serial garbage collection is enabled by default in a few modest hardware and operating system setups.

The -XX:+UseSerialGC command can be used to explicitly activate this.

  • Parallel Garbage Collector

This accelerates garbage collection by using many threads. In Java 8 or previous versions, this GC is the default for server class computers.

The command -XX:+UseParallelGC makes this possible.

  • CMS Garbage Collector

This works on certain pricey tasks alongside the application. By removing the lengthy delay that comes with a complete GC of parallel and serial collectors, this GC is optimized for low latency. This GC is no longer present in Java 14.

-XX:+UseConcMarkSweepGC may get rid of the CMS collector.

  • G1 Garbage Collector
  • Z Garbage Collector

--

--

Harshana Samarasinghe
CodeX
Writer for

“Truth can only be found in one place: the code.” - Associate Engineer — Java Technology at Virtusa Sri Lanka -