liveBook · Manning

Memory leak

As explained in Fast by Default

A failure to release memory that is no longer needed, causing memory usage to grow over time.

A memory leak retains memory after it is no longer useful, so a process grows instead of reaching a stable working level. The immediate symptom may be subtle: gradual growth triggers paging on desktop, operating-system termination on mobile, or an exhausted container limit on backend services. Restarts can temporarily relieve the slowdown or crash while concealing the underlying accumulation. section 5.2.3 section 6.2.1 section 7.2.4

Leaks develop through references and lifecycles that outlast their intended work. An event handler can capture a large object; unlimited undo history can retain every edit; request-scoped data can be stored for the process lifetime; error paths can skip connection cleanup; response bodies can prevent connection reuse; and caches can add unique entries without eviction. Removed UI elements or destroyed screens also remain when code still holds them. section 5.2.3 section 7.2.4 section 7.3.3 section 7.4.1

Treat memory as a bounded resource rather than an unlimited pool:

  • Set budgets at initial load, typical use, and extended operation; require usage to remain stable during the extended measurement. section 6.2.1
  • Bound caches, undo stacks, and retained state, then enforce eviction policies that keep use within limits over long sessions. section 5.2.3
  • Exercise view creation and destruction, media loading, and cache behavior; check whether memory returns to its starting level afterward. section 7.3.3
  • Profile on constrained physical devices before review, where generous emulator memory may hide leaks. section 7.3.3

Detecting a backend leak by repeated work

Take a memory snapshot before processing a batch, run the operation, then compare a second snapshot by line number. Repeat the operation ten times and compare the beginning and ending snapshots. A line that adds 50 KB once may be normal; a line that adds 50 KB on every request and grows proportionally points to accumulation. Entries that remain roughly stable indicate normal garbage-collection behavior instead. section 7.2.4

Listing 7.4 Python memory profiling with tracemalloc
import tracemalloc
 
tracemalloc.start() #A
snapshot_before = tracemalloc.take_snapshot() #B
 
process_batch(data) #C
 
snapshot_after = tracemalloc.take_snapshot()  #D
top_stats = snapshot_after.compare_to(       #E
    snapshot_before, 'lineno'
)
for stat in top_stats[:10]:                  #F
    print(stat)

This tracemalloc listing shows the snapshot-compare workflow: capture before and after a workload, compare allocations by line, then investigate the largest growing entries. section 7.2.4

Use platform-aware tooling to identify what retains memory. Browser developer tools can find detached elements in Electron and inspect Node.js with --inspect; dhat profiles Rust allocations in Tauri; Qt Creator understands parent-child ownership where generic growth reports may not identify the parent; and Flutter DevTools flags objects held longer than expected. The shared diagnostic question is whether a feature leaves memory behind after its lifecycle ends. section 7.4.1

How Fast by Default covers it

How deep it goes
Explains retention patterns, budget checkpoints, snapshot comparison, and platform profilers rather than garbage-collector internals.
Examples
Uses desktop event handlers and undo stacks, repeated backend requests, destroyed mobile screens, and detached Electron elements.
What it assumes
Assumes familiarity with application features, requests, and profiling tools while teaching leak detection through repeated measurement.
What you can do after
Diagnose/debug with snapshot comparisons, lifecycle checks, budgets, and platform-specific memory profilers.
How it connects
Connects memory leaks to memory budgets, garbage collection, caches, event listeners, undo history, and connection cleanup.

Also explained in

Compare how 9 books explain memory leak →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.