Hackforge Academy

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.

Code:


πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community