Category: java
Garbage Collection
Published on 22 Feb 2026
Explanation
Garbage Collection is the automatic memory
management process that removes unused objects.
Code:
String str = new String("Hello");
str = null; // Eligible for GC
Explanation
System.gc() is used to request JVM to
perform garbage collection.
Code:
System.gc();
Explanation
An object becomes eligible for garbage
collection when it has no references.
Code:
Object obj = new Object(); obj = null;
Explanation
finalize() method was used to perform
cleanup before GC
(deprecated in modern Java).
Code:
@Override
protected void finalize() throws Throwable {
System.out.println("Object collected");
}
Explanation
JVM manages heap memory and garbage
collection automatically.