Garbage Collection and Memory Leaks
40 min · Advanced
Garbage collection (GC) is the JVM finding objects that the program can no longer reach and reusing their memory, so you never call free yourself. Interviewers ask how GC decides what is garbage, whether you can force it, and how a Java program can still leak memory. Section 11 (Garbage Collection Basics, Garbage Collectors & JVM Options, References & Memory Leaks) taught it in depth; this lesson is the revision.
Topics in this lesson
- Reachability, GC roots and the generational heap
- Can you force GC? System.gc() and finalize
- Collectors in one line: Serial, Parallel, G1, ZGC
- Memory leaks in Java
- Strong, soft, weak and phantom references
Interview questions this lesson answers
- What is garbage collection in Java?
- How does the GC decide which objects to collect?
- What are GC roots?
- When does an object become eligible for garbage collection?
- Does Java use reference counting?
- What is the generational hypothesis?
- Explain Eden, survivor spaces and the old generation.
- What is the difference between minor, major and full GC?
- What is a stop-the-world pause?
- Can you force garbage collection?
Sign in free to continue this lesson
Create a free account to read every lesson, practise with the exercises and keep track of your progress.