Course map

Topic 008 / Advanced

Memory management

Chapter 1.2 · The Operating System and the Machine

What and why

A process's memory (→ 5) is split between the stack — automatic, per-thread, fast, fixed-size — and the heap — shared within the process, flexible-size, and either manually managed or reclaimed by a garbage collector. Every long-running service eventually has to answer where its memory is going; this topic covers how allocation and reclamation work and the specific ways both can go wrong, which is why it carries the Advanced tag. Builds on: → 5 (processes and threads).

Diagram

Stack Heap
Lifetime tied to the function call until explicitly freed or garbage-collected
Allocation automatic, on call explicit (new/malloc) or implicit via the runtime
Speed fast slower
Size fixed, limited (overflow risk) flexible (fragmentation risk)
Failure mode stack overflow (deep recursion) leak, fragmentation, or GC pause

How it works

  1. Every function call pushes a stack frame holding its local variables; returning pops it — automatic, and fast, but a fixed-size stack overflows under deep or unbounded recursion.
  2. Heap memory is requested explicitly, or implicitly by the language runtime, and must eventually be reclaimed — either by the program (manual free) or by a garbage collector.
  3. Manual reclamation is fast but has two classic failure modes: forgetting to free (a leak) and freeing twice or freeing something still in use (corruption).
  4. A garbage collector reclaims heap memory it can prove is unreachable, removing manual-free bugs — at the cost of pausing the program while it traces reachability, a direct source of tail latency (→ 100).
  5. A long-running service fragments its heap over years of small allocate/free cycles, leaving gaps too small to reuse — so memory use can grow even with zero true leaks.

Practical example

(illustrative). A service appends one small record to an in-memory list on every request and never clears it. At 10,000 requests/hour and 1 KB retained per request, that's roughly 10 MB/hour of growth; after 30 days (720 hours) roughly 7.2 GB has accumulated, and the process is eventually killed for using too much memory. The same bug leaks identically in a garbage-collected language — the collector cannot reclaim memory the program still holds a live reference to. The fix in both cases is the same: stop holding the reference (bound the list, or evict from it), not switch allocators.

When to use it

  • Use manual or precise memory control when: the language gives you that control and even brief GC pauses are unacceptable on the hot path (e.g., a latency-sensitive trading or control system).
  • Accept a garbage collector when: developer time and memory-safety matter more than microsecond-level pause variance — most backend services.
  • Avoid assuming "garbage collected" means "leak-free" — unreachable-but-still-referenced memory leaks in any language.
  • Failure scenarios: an unbounded cache or queue with no eviction policy growing until the process is killed (→ 86 solves this specifically for caches); heap fragmentation making allocation slow even while plenty of memory is technically free; GC pause spikes surfacing as tail latency under load (→ 100).
  • Simpler alternative: before tuning a garbage collector or writing a custom memory pool, check whether the real cause is simply holding a reference too long — an unbounded cache, a listener that was never unregistered. Fixing the reference lifetime is almost always simpler than fixing the allocator.

Practice

(mechanism — build a tiny version; 20–30 min, no lab services). Write a program that allocates and appends to a growing in-memory list in a loop, never clearing it, and watch its process memory grow using an OS-level tool (e.g., your system's process monitor). Then cap the same loop with a bounded structure — a fixed-size ring buffer, or evict-oldest once a size limit is hit — and confirm memory plateaus instead of growing. Done when you've shown the growth, then shown it capped, using the same measurement each time.

Key takeaway

Stack memory is automatic and bounded; heap memory is flexible but must be reclaimed, manually or by a collector, and both have distinct failure modes. Done when you can: show unbounded memory growth from a held reference, then eliminate it by bounding the structure, not by changing the allocator. Used next in → 86 (cache eviction), → 100 (tail latency from GC pauses), → 103 (profiling and bottleneck analysis).

References

None — this topic makes no external claims.

Source directory

Search the guide