1. Concept library
  2. Java & Spring Boot
  3. Module 3: JVM internals

Concept

Garbage collection

How the JVM finds objects nobody can reach any more and reclaims their memory, and what that costs your application.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Explain how the JVM decides that an object is reachable
  2. Identify the GC roots in a short program
  3. Explain why most collectors split the heap into generations

In practice

A few lines that show Garbage collection doing its job.

Bash
# see what the collector is doing
java -Xlog:gc -Xmx512m -jar app.jar

# a typical line: pause time is what users feel
# [2.113s][info][gc] GC(7) Pause Young (Normal) (G1 Evacuation Pause) 230M->41M(512M) 6.2ms

The common mistake

Watch for

Calling System.gc() to "free memory" in application code. It is a hint the JVM may ignore, and when honoured it triggers a full collection that pauses everything.

What to know first

Your roadmap puts these before Garbage collection, unless you already know them.

How it stays learned

Once a session shows you understand Garbage collection, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

The review card

What you are asked on review day.

Java & Spring Boot · JVM internals

When does an object become eligible for garbage collection in Java?

Show answer

When no chain of references from a GC root (a live thread stack, a static field, a JNI reference) reaches it any more. Setting a variable to null only helps if that was the last such path.

Where it leads

Topics that build on Garbage collection.

Related