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.