What is Garbage Collection?
Fast recall
Quick revision
Core answer
free from ordinary Java code, but retained references can still cause memory leaks and collection timing is not guaranteed.Basic understanding of Java's automatic memory management and why it exists
Saying GC deletes all unused variables — GC only handles heap objects, not stack variables
GC avoids explicit deallocation and use-after-free in ordinary Java, but it cannot reclaim an object that application logic still keeps reachable
Interview-ready guide
Interview answer
Garbage collection automatically reclaims heap space from objects that are no longer reachable from GC roots. Roots include places such as active thread frames, static fields, and JNI references. Unreachable means eligible, not immediately deleted—the JVM chooses collection timing and algorithm. This removes explicit deallocation from ordinary Java and avoids dangling references to reclaimed Java objects. It does not prevent a logical leak: a growing cache or static list keeps its elements reachable. Collectors can also pause application threads for some phases, while other work may run concurrently. In production, inspect allocation, retained heap, GC logs, and pause latency before deciding whether code, heap sizing, or collector settings should change.
What is Garbage Collection
Direct Answer
Garbage collection is automatic heap-memory management. A collector traces reachability from GC roots such as active thread frames, static fields, and JNI references, then may reclaim objects that are no longer strongly reachable. It removes explicit free from ordinary Java code, but retained references can still cause memory leaks and collection timing is not guaranteed.
Code Example
public class OrderService {
private OrderRepository orderRepo;
public void processOrders() {
// Object created on heap
var tempOrders = orderRepo.findAll();
for (var order : tempOrders) {
processOrder(order);
}
// tempOrders goes out of scope here
// If no other references exist, eligible for GC
}
public void createTemporaryReport() {
var report = new StringBuilder();
report.append("Sales: ").append(calculateSales());
// report object eligible for GC after method returns
// GC will reclaim this memory automatically
}
}
Common Mistakes & Pitfalls
- Treating GC as universal resource cleanup: GC reclaims eligible managed objects; files, sockets, database connections, and other owned resources still need explicit closing.
- Assuming immediate reclamation: Removing one reference may make an object eligible, but only when no strong path from a GC root remains. Collection timing is not guaranteed.
- Ignoring runtime cost: Collector work uses CPU, and stop-the-world phases suspend Java application threads in that JVM. Measure allocation, pauses, and application latency instead of assuming a fixed effect.
Check your understanding
Answer these without looking back at the article.
- Can you force garbage collection in Java?
- What makes an object eligible for garbage collection?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.