Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Understanding Java Garbage Collection: How It Works & Why It Matters

What Is Java Garbage Collection?

Java’s Garbage Collector (GC) automatically manages memory, freeing developers from manual de‑allocation. When an object has no live references, the GC reclaims its heap space, preventing memory leaks that can otherwise crash applications.

In Java, objects are created on the heap via the new operator. The runtime keeps track of references; when the last reference disappears, the object becomes eligible for collection. The GC runs periodically in the background, making your code simpler and safer.

Unlike C, where free() is required, Java eliminates the risk of dangling pointers and undefined behavior, reinforcing reliability and security.

All objects in Java live in the heap section of memory – the area that GC scans.

Hands‑On Example: Watching GC in Action

Step 1 – Copy the code below into an editor:

class Student {
    int a;
    int b;

    public void setData(int c, int d) {
        a = c;
        b = d;
    }

    public void showData() {
        System.out.println("Value of a = " + a);
        System.out.println("Value of b = " + b);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student();
        s1.setData(1, 2);
        s2.setData(3, 4);
        s1.showData();
        s2.showData();
        //Student s3;
        //s3 = s2;
        //s3.showData();
        //s2 = null;
        //s3.showData();
        //s3 = null;
        //s3.showData();
    }
}

Step 2 – Compile and run the program. Initially, two Student objects and two reference variables are created. Refer to the diagram below.

Understanding Java Garbage Collection: How It Works & Why It Matters

Step 3 – Uncomment lines 20‑22, then re‑run. Now s3 points to the same object as s2. The diagram shows both references sharing one object.

Understanding Java Garbage Collection: How It Works & Why It Matters

Step 4 – Uncomment lines 23‑24. Setting s2 to null leaves s3 as the sole reference, so the object is still live.

Understanding Java Garbage Collection: How It Works & Why It Matters

Step 5 – Uncomment lines 25‑26. With both s2 and s3 set to null, the object becomes eligible for GC. The final diagram illustrates this state.

Understanding Java Garbage Collection: How It Works & Why It Matters

Making an Object Eligible for Collection

  1. Assign its reference variable to null. The GC will then reclaim the memory when it runs.
  2. Primitive types (int, double, etc.) are not objects and cannot be null.

Understanding Java Garbage Collection: How It Works & Why It Matters

Note: Explicit finalizers are discouraged; rely on GC instead.

Java

  1. Java Collection Interface: Core Concepts & Essential Methods
  2. Java Explained: Definition, Features, Platform Overview & Versions
  3. JVM Explained: What It Is and Its Architecture
  4. Mastering Abstraction in OOP: Java Abstract Classes & Methods Explained
  5. Understanding Java Garbage Collection: How It Works & Why It Matters
  6. Java vs. Scala: Key Differences, Pros, and Cons for Modern Developers
  7. Mastering Java Collections: A Comprehensive Guide to Efficient Data Structures
  8. Java 10: Introducing the Garbage‑Collector Interface for Flexible, Efficient Memory Management
  9. Java 10 Enhancements: Allocate Heap on NV‑DIMM with -XX:AllocateHeapAt
  10. Java 9 Collection Factory Methods: Simplify Immutable List, Set & Map Creation