DEV Community

Cover image for Understanding Garbage Collection in Java: What, Why, and How
Bellamer
Bellamer

Posted on

Understanding Garbage Collection in Java: What, Why, and How

Memory management is an essential aspect of programming, and Java provides developers with an automatic garbage collection mechanism to handle it efficiently. Unlike languages such as C or C++, where developers must manually allocate and free memory, Java’s garbage collector (GC) takes care of reclaiming unused memory, reducing the risk of memory leaks.

In this blog post, we’ll explore what garbage collection is, why it’s important, how it works in Java, and some best practices for working with it.

What is Garbage Collection?

Garbage collection in Java is the process of automatically identifying and reclaiming memory occupied by objects that are no longer reachable or needed by the application. These objects are marked as “garbage,” and their memory is recycled for reuse.

Key Features:

  • Automatic: The Java Virtual Machine (JVM) automatically manages memory.
  • Non-deterministic: The exact time when garbage collection occurs cannot be predicted.
  • Mark-and-sweep: Most garbage collection algorithms involve marking reachable objects and sweeping away unreachable ones.

Why is Garbage Collection Important?

  1. Memory Optimization: Frees up unused memory to ensure the application has sufficient space to continue running.
  2. Simplifies Development: Eliminates the need for manual memory management, reducing developer effort and errors.
  3. Prevents Memory Leaks: Identifies and cleans up unused objects, ensuring memory is not unnecessarily consumed.

How Does Garbage Collection Work in Java?

Heap Memory and Object Lifecycles

Java’s garbage collector operates on heap memory, where all objects are stored. Objects have varying lifetimes, and the garbage collector ensures that:

  • Objects still in use remain in memory.
  • Objects no longer reachable (i.e., have no references) are marked for removal.

The Generational Garbage Collection Model

Java uses a generational model to optimize garbage collection based on the assumption that:

  • Most objects are short-lived (e.g., temporary objects created in a method).
  • Long-lived objects are less likely to become garbage.

The heap is divided into the following regions:

  • Young Generation (or New Generation):

    • Purpose: Holds short-lived objects (e.g., temporary variables, loop variables).
    • Divisions:
      • Eden Space: Where new objects are allocated.
      • Survivor Spaces (S0 and S1): After surviving one or more garbage collections in Eden, objects are moved to Survivor spaces.
    • Garbage Collection:
      • Minor GC is used to clean up the Young Generation.
      • Happens frequently as most objects here have a short lifespan.
    • Promotion: If an object survives a certain number of GC cycles, it is promoted to the Old Generation.
  • Old Generation (or Tenured Generation):

    • Purpose: Stores long-lived objects, such as those that persist throughout the application lifecycle.
    • Garbage Collection:
      • Major GC (or Full GC) occurs less frequently but takes longer because it involves the entire heap.
      • Objects from the Young Generation are promoted here if they survive enough GC cycles.
    • Size: Typically larger than the Young Generation.
  • Permanent Generation (PermGen) [Pre-Java 8]:

    • Purpose:
      • Stores metadata about classes, methods, and other JVM-internal structures.
      • Also stores static fields and string literals.
    • GC:
      • Not managed as dynamically as the heap (Young/Old Generations), leading to potential OutOfMemoryError: PermGen Space if the space was exhausted.
      • Rarely collected.
    • Deprecation:
      • Removed in Java 8 and replaced with Metaspace.
      • Metaspace is allocated in native memory and grows dynamically, avoiding many PermGen-related issues.

Dividing memory into generations allows GC to optimize its performance by focusing on:

  • Frequent, fast cleanups for short-lived objects (Young Generation).
  • Less frequent but more thorough cleanups for long-lived objects (Old Generation).

Post-Java 8 Changes (Metaspace):

  • The Metaspace replaced PermGen and is located in native memory rather than the JVM heap.
  • No fixed size like PermGen; it can grow as needed, subject to available system memory.

Phases of Garbage Collection

  • Marking:
    • Identifies all reachable objects by traversing references from GC roots (e.g., local variables, active threads).
  • Sweeping:
    • Reclaims memory occupied by unreferenced objects.
  • Compacting (optional):
    • Rearranges objects in memory to eliminate fragmentation.

Garbage Collection Algorithms in Java

The JVM provides multiple garbage collection algorithms, which can be selected based on the application’s requirements.

  • Serial Garbage Collector

    • Best for: Single-threaded environments or small heaps.
    • Mechanism: Uses a single thread for garbage collection.
    • Key Characteristic: Simpler but causes application pauses during GC.
  • Parallel Garbage Collector (Throughput Collector)

    • Best for: Applications requiring high throughput (default in Java 8).
    • Mechanism: Uses multiple threads for GC in both young and old generations.
    • Key Characteristic: Reduces pause time but may impact CPU availability.
  • G1 Garbage Collector (Garbage First)

    • Best for: Applications needing low-latency GC (default in Java 9 and later).
    • Mechanism: Divides the heap into regions and prioritizes collecting regions with the most garbage.
    • Key Characteristic: Balances pause time and throughput.
  • Z Garbage Collector (ZGC)

    • Best for: Large heap applications requiring ultra-low-latency GC.
    • Mechanism: Performs most of the work concurrently, with pauses in the millisecond range.
    • Key Characteristic: Ideal for heaps up to terabytes in size.
  • Shenandoah Garbage Collector (Shenandoah GC)

    • Best for: Applications with medium to large heaps requiring low-latency GC.
    • Mechanism: Performs concurrent garbage collection and compaction, minimizing pause times by running most operations alongside application threads.
    • Key Characteristic: Focuses on low pause times (typically a few milliseconds) by compacting memory while the application is running.

Code Example: How Objects Become Garbage

Here’s a simple example to demonstrate how objects become unreachable and are eligible for garbage collection:

public class GarbageCollectionDemo {
    public static void main(String[] args) {
        // Object 1 is created
        Person person1 = new Person("Alice");

        // Object 2 is created
        Person person2 = new Person("Bob");

        // Reference of person1 is reassigned
        person1 = person2; // Object "Alice" is now unreachable

        // Both person1 and person2 point to "Bob"
        System.out.println(person1.name); // Output: Bob

        // At this point, "Alice" object is eligible for garbage collection
    }
}

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. The object "Alice" becomes unreachable when person1 is reassigned to person2.
  2. The garbage collector can now reclaim the memory occupied by "Alice".

How to Trigger Garbage Collection

While you cannot force garbage collection, you can suggest it to the JVM using:

System.gc(); 
Enter fullscreen mode Exit fullscreen mode

Why Avoid Explicit Calls?

  • Garbage collection is automatic and optimized by the JVM.
  • Calling System.gc() may disrupt the garbage collector’s efficiency.

Common Garbage Collection Issues

  • Memory Leaks

Occurs when objects remain referenced unnecessarily, preventing garbage collection.

Example:

import java.util.ArrayList;
import java.util.List;

public class MemoryLeakExample {
    static List<Object> list = new ArrayList<>();

    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            list.add(new Object()); // Keeps adding objects to the static list
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, the list retains references to all objects, making them ineligible for GC.

  • OutOfMemoryError

Occurs when the heap is full, and no memory can be reclaimed.

Solution:

Increase heap size with JVM options:

java -Xmx1024m MyApp
Enter fullscreen mode Exit fullscreen mode

Best Practices for Garbage Collection

  • Avoid Unnecessary Object Creation:
    • Reuse objects where possible.
    • Use lightweight objects for short-term data.
  • Nullify Unused References:
    • Set references to null when they are no longer needed.
myObject = null;
Enter fullscreen mode Exit fullscreen mode
  • Use Collections Wisely:
    • Remove unused elements from collections like List or Map.
  • Monitor Garbage Collection:
    • Use tools like JConsole or VisualVM to monitor GC activity.
  • Choose the Right Garbage Collector:
    • Tailor the GC based on application needs using JVM options (e.g., -XX:+UseG1GC for the G1 collector).

Key Takeaways

  1. Automatic Memory Management: Garbage collection is a built-in mechanism in Java that simplifies memory management.
  2. Generational Model: GC by handling short-lived and long-lived objects differently.
  3. Choose the Right GC Algorithm: Match the garbage collector to your application’s needs for throughput or low latency.
  4. Monitor and Optimize: Regularly analyze heap usage and garbage collection metrics to prevent memory-related issues.

By understanding and leveraging Java’s garbage collection effectively, you can ensure your applications run efficiently and are free from memory leaks and performance bottlenecks.

Top comments (1)

Collapse
 
kunalgaurav18 profile image
Kunal Gaurav

Superb ... as always!! 😇