3.7 · Leaks, abandoned memory, footprint, and the tools

Knowing the theory is half the job. The other half is finding, in a real app, which kind of memory problem you have, because the kinds look alike and need different tools.

Mental model. First decide whether the memory is unreachable, reachable but unwanted, or simply too much at once. That one question picks the tool and the fix.

Kinds of problems

Leak, abandoned memory, fragmentation and peaks are not the same thing.

  1. What is the difference between a leak, abandoned memory, fragmentation and a memory spike? 3.7.1 · L3
    A leak is memory nothing can reach any more, typically a retain cycle: no pointer from a live root leads to it, and it can never be freed. Abandoned memory is…

Tools and tests

The memory graph, Instruments and a unit test that stops leaks coming back.

  1. How do you find a retain cycle with the Memory Graph debugger? 3.7.2 · L3
    Reproduce the problem first: open and close the suspect screen a few times. Then pause and choose Debug Memory Graph in Xcode. The navigator lists every live…
  2. How do you use Instruments’ Allocations, Leaks and VM Tracker to track a memory problem? 3.7.3 · L5
    Allocations records every allocation with its call stack. The method that finds growth is generations: take a baseline, repeat the action (open and close a…
  3. How do you write a unit test that catches a leak? 3.7.4 · L3
    The idea is to hold a weak reference, release every strong one, and assert the weak reference became nil. Create the object under test in an inner scope (or an…

Footprint on a phone

What actually gets an app killed.

  1. What is memory footprint, and what gets an iOS app killed? 3.7.5 · L3
    iOS has no swap file. When memory runs short the system terminates processes (jetsam), using each process’s footprint: roughly its dirty pages (memory your…

More in Memory, ARC & runtime