Member-only story

Garbage Collector in Java

Saurav Kumar
7 min readSep 20, 2023

In Java, the garbage collector is a part of the Java Virtual Machine (JVM) responsible for automatically managing memory by reclaiming memory occupied by objects that are no longer referenced by the program. Here are some key points about the Java garbage collector:

  1. Automatic Memory Management: The Java garbage collector automatically frees up memory occupied by objects that are no longer reachable by the program, eliminating the need for manual memory management.
  2. Object Reachability: The garbage collector determines whether an object is reachable or not. Objects that are no longer reachable from the program’s root objects (e.g., references from the main method or other active objects) are considered eligible for garbage collection.
  3. Types of Collectors: Java provides different garbage collection algorithms, including the Serial, Parallel, CMS (Concurrent Mark-Sweep), G1 (Garbage First), and ZGC (Z Garbage Collector). Each collector has its own strengths and is suited for different types of applications.
  4. Garbage Collection Process: The process typically involves phases like marking, sweeping, and compacting, depending on the collector used. During marking, live objects are identified. During sweeping, memory occupied by unreachable objects is released. Compacting may be done to defragment memory.

--

--

Saurav Kumar
Saurav Kumar

Written by Saurav Kumar

Experienced Software Engineer adept in Java, Spring Boot, Microservices, Kafka & Azure.

No responses yet