Hunt Retain Cycles with the Memory Graph Debugger

Xcode's Memory Graph Debugger is a visual map of your app's live objects and their relationships. Use it to hunt down memory leaks, especially retain cycles where objects won't deallocate.
WHY IT EXISTS Automatic Reference Counting (ARC) manages most memory automatically, but it can't resolve 'retain cycles'—when two or more objects hold strong references to each other in a closed loop. This creates memory leaks. Manually tracing these reference chains in code is difficult and time-consuming, so a visual tool is needed.
THE MENTAL MODEL Think of the Memory Graph Debugger as a social network for your app's objects. Each object is a person, and a reference is a connection. A retain cycle is a clique where everyone holds onto each other, so no one can leave the party (i.e., be deallocated from memory). The debugger lets you see these cliques and find out who is holding onto whom.
HOW IT WORKS While your app is running, you activate the debugger. It pauses execution and takes a snapshot of the heap, showing all objects currently in memory. It renders this as a graph where nodes are objects and edges are the references (strong, weak, or unowned) between them. The tool automatically highlights nodes with purple exclamation marks to indicate a likely leak, showing the problematic reference path.
WHEN TO USE IT Use this tool when you observe your app's memory footprint consistently growing over time and never returning to a baseline, even after you dismiss views or complete tasks. It is the go-to tool for diagnosing and fixing retain cycles, which are common with closures capturing self strongly or improperly configured delegate patterns.
WHEN NOT TO USE IT This is a specialized tool for memory leaks, not general performance. For issues like slow UI rendering, high CPU usage, or excessive disk I/O, other tools in Instruments like the Time Profiler or Allocations are more appropriate starting points. The memory graph is about object relationships, not execution speed.
ONE CANONICAL EXAMPLE A ParentViewController has a strong reference to a ChildViewController. The child has a delegate property to communicate back to the parent. If this delegate property is also strong (the default), you have a retain cycle: Parent -> Child -> Parent. Neither can be deallocated. The memory graph will show this loop clearly. The fix is to declare the delegate property as weak.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.