Skip to content
Ameen Abozaid
MID-002

Memory management

  • 7 Lessons
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Heap generations
  • allocation
  • GC roots
  • garbage collectors
  • GC logging

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Explain generational collection, allocation, promotion, and GC roots.
  2. Compare G1, ZGC, and Parallel GC and choose one for a latency- or throughput-focused service.
  3. Enable and read GC logs, and identify allocation pressure and long pauses.
  4. Size heap and container memory limits so the JVM respects the container.
  5. Diagnose a memory leak by comparing heap dumps.

Lessons

  1. 01How the JVM allocates objects
  2. 02Generations, GC roots, and reachability
  3. 03Collectors: G1, ZGC, Parallel
  4. 04Reading GC logs
  5. 05The JVM inside a container
  6. 06Memory leaks in managed languages
  7. 07Demo: finding a leak with two heap dumps

Lab and exercises

Lab

MID-002-LAB1 · Leaky cache

Outcomes: 1, 3, 5 (and 4) · Time: ~4 h A planted unbounded cache in Shop leaks under load. Enable GC logging, run load, find the rising floor, take two heap dumps, identify the retaining path, fix it (bounded cache), and prove the fix with a second run. Set heap with MaxRAMPercentage for a 1 GiB container. Acceptance: before/after heap charts.

Exercises (assessment: diagnose a provided GC log)

IDOutcomeTask
MID-002-EX011Explain allocation, promotion, and GC roots using a Shop checkout request as the example.
MID-002-EX022Pick a collector for: nightly price import; checkout API p99 under 50 ms on a 16 GB heap; typical 2 GB web API. Justify each.
MID-002-EX033Enable GC logging on Shop; report allocation rate and average young pause over 5 minutes.
MID-002-EX043Given a provided GC log, state whether it shows a leak, allocation pressure, or neither, citing lines.
MID-002-EX054A pod has a 2 GiB limit and -Xmx2g. Explain the risk and give a corrected setting.
MID-002-EX065List the steps to compare two heap dumps and name three leak sources to check first.
Show solutions
  • EX01 Order objects are allocated in Eden by pointer bump; request-scoped objects die in the next young GC; objects still referenced from static or long-lived structures survive and are promoted. Roots: stacks, statics.
  • EX02 Import: Parallel (throughput). 16 GB strict latency: ZGC. Typical API: G1 (default), tune only after measuring.
  • EX03 Use -Xlog:gc*; allocation rate is roughly the sum of (before minus previous after) divided by time; average the young pause durations.
  • EX04 A leak shows the post-collection used size climbing across mixed/full GCs; pressure shows frequent young GCs with a flat floor; "neither" shows flat floor and short pauses.
  • EX05 Heap alone fills the limit; metaspace, stacks and buffers push it over, so the container is OOM-killed. Use -XX:MaxRAMPercentage=70 or -Xmx1400m.
  • EX06 Take dump A and later dump B; compare class histograms; inspect the dominator tree and path to GC roots. Check static collections, caches, and ThreadLocal/listeners.
  • Outcome 1 is also covered by Q01-Q03 and outcome 5 by the lab.

Quiz

Outcomes: O1 generations and roots · O2 collectors · O3 GC logs · O4 containers · O5 leaks.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Which is a GC root?O1
  2. 2The generational hypothesis says:O1
  3. 3An unreachable object cycle is:O1
  4. 4Best fit for a nightly batch import:O2
  5. 5Which collector offers very short concurrent pauses on large heaps?O2
  6. 6612M->140M(1024M) in a GC line shows:O3
  7. 7A rising post-GC heap floor suggests:O3
  8. 8A pod is OOM-killed (exit 137) with heap below -Xmx. Likely cause:O4
  9. 9The better way to size heap in containers is:O4
  10. 10Fixing a leak by raising -Xmx will:O5

Practical part

Six exercises MID-002-EX01 to EX06 including the provided GC-log diagnosis, plus the lab with before/after charts. Pass: at least five exercises correct and the leak located and proven fixed.

Common mistakes

Raising -Xmx to "fix" a leak; ignoring container memory limits; tuning flags without measurement.

Assessment

Diagnose a provided GC log.

Career relevance

Production troubleshooting (SRC-015) and capacity planning (SRC-004).

Type to search levels, phases, modules and lessons.