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








