Failures and antipatterns · Intermediate

Memory leaks: retaining memory after useful work ends

Separate retained objects, caches and allocator behavior when process memory grows.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Growth is a symptom, not the diagnosis
  2. An unremoved subscription
  3. Compare repeated workloads
  4. Containment versus repair
  5. Ask Logeia about this articleCheck your understanding

Growth is a symptom, not the diagnosis

A memory leak retains memory after it is no longer needed. In manually managed code this can be a missing release; in garbage-collected code it can be an unnecessary reference keeping an object reachable. Garbage collection does not remove the need to manage object lifetimes.

Growth alone is not a diagnosis. Cache warm-up and allocator retention can also increase process memory. Relate resident memory to workload and completed operations, then inspect what remains after cleanup or collection. One rising RSS sample cannot distinguish all these explanations.

An unremoved subscription

Consider registering an event subscription whenever a screen opens but never removing it when the screen closes. The event source retains the callback, and the callback may retain a large screen data structure. That structure remains reachable after the user no longer needs it.

Repeat the open-close cycle and compare subscription counts and retained objects. Following the retaining path can reveal the owner preventing cleanup more directly than ranking objects only by size. The important relationship is lifetime after useful work ends, not merely the existence of a large allocation.

Compare repeated workloads

Run the same bounded workload repeatedly and compare the memory remaining after each completed cycle. Use heap snapshots, allocation records or retaining-path tools appropriate for the language and runtime. Separate transient peaks from a baseline that continues rising between comparable cycles.

Operating-system memory, runtime heap capacity and live application objects are different quantities. An allocator can retain freed regions for reuse, so removal of an unwanted object does not require an immediate RSS drop. Match the diagnostic tool to the specific lifetime claim being tested.

Containment versus repair

Restarts and memory limits can contain impact without repairing the lifetime bug. Reproduce the same operation after the change and check whether unnecessary retention disappeared, rather than merely increasing the time before another failure. Resource containment and root-cause repair have different acceptance criteria.

Memory captures can contain application data and secrets. Prefer synthetic workloads and appropriate private storage for heap dumps. A diagnosis should not create a new exposure by publishing raw process memory. Report only the observations needed to explain the retaining path and the verified correction.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 16

Understand the systems behind the web · STEP 6

Prepare for AI systems · STEP 6

Choose a learning path