← Lessons

quiz vs the machine

Gold1450

System Design

Memory Profiling

Spotting leaks, bloat, and churn by tracking where allocations live and die.

5 min read · core · beat Gold to climb

What memory profiling shows

A memory profiler tracks where allocations happen and how long objects live. It answers two questions, how much memory each path uses and whether memory is being released.

Three problems it finds

  • Leaks are objects that are allocated but never freed, so usage grows without bound.
  • Bloat is correct but wasteful structures, like a map far larger than needed.
  • Churn is rapid allocate and free, which is freed but stresses the allocator and collector.

Heap snapshots

Comparing two snapshots over time highlights objects that keep growing. Following the retaining path shows what reference keeps them alive, which is usually the root cause of a leak.

Allocation profiles

An allocation profile attributes bytes to the call stacks that created them, much like a CPU flamegraph but for memory. Wide frames point at the hot allocation sites worth reducing or pooling.

Fixes

  • Break references that pin objects, such as an unbounded cache.
  • Reuse buffers to cut churn.
  • Right size collections to remove bloat.

Key idea

Compare heap snapshots and read allocation profiles to separate leaks, bloat, and churn, then fix the retaining path or the hot allocation site.

Check yourself

Answer to earn rating on the learn ladder.

1. Comparing two heap snapshots over time mainly helps you find what?

2. What is allocation churn?