Garbage Collection
Automatic memory management
Interview Relevant: GC algorithms and tuning
7 min read
Garbage Collection
Garbage Collection (GC) automatically reclaims memory occupied by unreachable objects.
- Prevents memory leaks
- Runs automatically
- Uses reachability analysis
Code Examples
Object becomes eligible for GC
java
1
2User u = new User();
3u = null; // Eligible for GC
4 Use Cases
- Automatic memory management
Common Mistakes to Avoid
- Calling System.gc() explicitly
- Assuming GC runs immediately