← Back to libraryQuestion 14 of 273
JavaIntermediate

Garbage Collection (GC)

📖 Detailed Explanation:

GC is the process of automatically identifying and deleting objects in the heap that are no longer reachable (no references pointing to them). This prevents manual memory management errors like memory leaks and dangling pointers found in C/C++. The GC runs periodically and can be triggered by memory pressure or explicitly requested (though not guaranteed).

🌍 Real-World Example:

A waiter in a restaurant. When you finish your meal and leave the table (remove the reference), the waiter (GC) eventually comes and clears the plate (frees memory). The waiter only clears plates from abandoned tables, not from tables people are still using.

🎯 Scenario-Based Interview Question:

Can you force the Garbage Collector to run immediately by calling System.gc()? Explain the behavior.