What is Garbage Collection in Java and How Does It Work?
Garbage Collection (GC) is the automatic memory management process that reclaims memory by deleting objects no longer referenced.
How GC Works - Basic Concept
public class GCDemo {
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
Student s3 = new Student();
System.out.println("Objects created");
s1 = null;
System.out.println("s1 set to null - eligible for GC");
s2 = new Student();
System.out.println("s2 reassigned - original object eligible for GC");
System.gc();
System.out.println("System.gc() called - only a suggestion");
Runtime.getRuntime().gc();
System.out.println("Runtime.getRuntime().gc() called");
Runtime runtime = Runtime.getRuntime();
System.out.println("Total Memory: " + runtime.totalMemory() / (1024 * 1024) + " MB");
System.out.println("Free Memory: " + runtime.freeMemory() / (1024 * 1024) + " MB");
System.out.println("Used Memory: " + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024) + " MB");
}
}
class Student {
int id;
String name;
@Override
protected void finalize() throws Throwable {
System.out.println("Student object " + this + " is being garbage collected");
super.finalize();
}
}
Heap Memory Structure (Generational GC)
public class HeapStructure {
public static void main(String[] args) {
Runtime runtime = Runtime.getRuntime();
System.out.println("=== JVM Memory Information ===");
System.out.println("Max Memory: " + runtime.maxMemory() / (1024 * 1024) + " MB");
System.out.println("Total Memory: " + runtime.totalMemory() / (1024 * 1024) + " MB");
System.out.println("Free Memory: " + runtime.freeMemory() / (1024 * 1024) + " MB");
java.lang.management.MemoryMXBean memoryBean =
java.lang.management.ManagementFactory.getMemoryMXBean();
System.out.println("\n=== Heap Memory Usage ===");
System.out.println("Heap Memory: " + memoryBean.getHeapMemoryUsage());
System.out.println("Non-Heap Memory: " + memoryBean.getNonHeapMemoryUsage());
}
}
GC Process Demonstration
import java.util.*;
public class GCProcessDemo {
static class LargeObject {
int[] data = new int[1000];
String name;
LargeObject(String name) {
this.name = name;
}
}
public static void main(String[] args) throws InterruptedException {
List list = new ArrayList<>();
System.out.println("=== Simulating GC Process ===");
for (int cycle = 1; cycle <= 3; cycle++) {
for (int i = 0; i < 5000; i++) {
list.add(new LargeObject("Obj-" + cycle + "-" + i));
}
if (cycle % 2 == 0) {
list.clear();
}
System.gc();
Thread.sleep(500);
}
System.out.println("=== GC Process Complete ===");
}
}
GC Algorithms
- Serial GC – Single thread, simple apps
- Parallel GC – Multi-threaded, high throughput
- G1 GC – Default from Java 9, balanced
- ZGC – Ultra-low latency
- Shenandoah – Low pause time GC
Object Eligibility for GC
- Null reference
- Reassignment
- Out of scope
- Island of isolation
Best Practices
- Avoid
System.gc() - Use try-with-resources
- Avoid finalize() (deprecated)
- Use WeakHashMap for cache
Master Java memory management with Online Learner!
0
likes
Your Feedback
Help us improve by sharing your thoughts
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
