# Debug Leaks Using Xcode Memory Graph Remotely

Canonical: https://figment.so/blog/mac-memory-graph-debugger-remote

Run the app in the Simulator over a remote desktop session, push and pop the suspect screen five or six times, then click Debug Memory Graph. An Apple Silicon Mac VM ($39 a month, $6 a day) runs current Xcode; the Intel Mac VM covers Xcode 26.3 or earlier.

**If your iOS app's memory climbs with every navigation push and never drops back, run it in the iOS Simulator on a remote Mac, push and pop the suspect screen five or six times, then click Debug Memory Graph in Xcode.** The Memory Graph Debugger needs a live Xcode window, so connect through encrypted remote desktop. Apple's references are [Finding memory leaks with the Memory Graph Debugger](https://developer.apple.com/documentation/xcode/diagnosing-and-resolving-bugs-in-your-running-app) and [Gathering information about memory use](https://developer.apple.com/documentation/xcode/gathering-information-about-memory-use).

A retain cycle is a closed loop of strong references that keeps ARC from deallocating objects. The debugger pauses your app and draws every live heap object as a node, so you can see which references form the loop.

## What does the Memory Graph Debugger capture?

It freezes the running app and snapshots the heap: every allocated object, its class, address and size, and the strong and weak reference edges between objects. The result is a node graph in the center panel, and the navigator on the left lists objects grouped by type with instance counts. If Xcode detects a suspected cycle, it marks the involved nodes with a purple exclamation badge. The inspector on the right shows class name, address, size and, when malloc stack logging is on, a backtrace of where the object was allocated.

A cycle appears as looping arrows between two or more nodes. A `ViewController` that holds a strong reference to a `DataManager`, while `DataManager` holds one back, produces exactly that loop. Neither object can be released, so both outlive the navigation pop.

## How do I reach the Xcode window on a remote Mac?

Open an encrypted remote desktop session to your Figment Mac and launch Xcode there. The iOS Simulator runs inside the same desktop session, so you can build, launch, navigate to the screens that trigger growth, and pause for the graph in one window. If you prefer a plain VNC-style client, [Access Xcode GUI Over VNC on a Remote Mac](/blog/mac-vnc-xcode-gui-remote) covers the options.

The `leaks` command-line tool also works in an SSH terminal against a `.memgraph` captured anywhere, which is useful for offline triage.

## When should I trigger the capture?

After the suspected leak has had time to accumulate, not at app launch. A reliable workflow:

1. Build and run the app in the Simulator with a **Debug** configuration. Release builds strip type metadata the graph needs.
2. Navigate into the suspect screen: push the view controller, then pop it. Repeat five or six times.
3. Watch the **Memory** gauge in Xcode's Debug navigator (⌘7). If memory never returns to its pre-push baseline, you likely have a cycle.
4. Click the **Debug Memory Graph** button in the debug bar (the three-circle icon) to pause and capture. Check Xcode's Help menu for the keyboard shortcut in your installed version.

Capturing after several push/pop cycles means the objects that should have been released are the ones still present, instead of being hidden among one-time launch allocations.

## How do I read the graph and find the cycle?

Solid arrows are strong references and dashed arrows are weak ones, and a retain cycle is always a closed loop of solid arrows. Select the purple-badged object in the navigator and trace its outgoing solid arrows in the center panel until they loop back.

A common Swift case is a closure stored as a property that captures `self` strongly. The class holds the closure (strong) and the closure holds the class (strong). Fix it with a `[weak self]` capture list; the graph shows this as a two-node loop with both arrows solid.

Also check whether an instance count in the navigator matches your push count. If you pushed five times and see five `PhotoDetailViewController` instances alive, all five are leaked.

## What is the checklist for each session?

- [ ] **Debug build configuration.** Release builds hide the type names you need.
- [ ] **Malloc stack logging on.** Edit Scheme → Diagnostics → Malloc Stack Logging → Live Allocations. Required to see allocation backtraces in the inspector.
- [ ] **Reproduce the pattern.** Push and pop, or trigger the suspected operation, at least 3 to 5 times before capturing.
- [ ] **Let memory settle.** Wait a few seconds after the last action so deferred deallocations complete.
- [ ] **Filter the navigator by your module.** Type your module name in the navigator search box to hide system objects.
- [ ] **Instance count matches iterations.** Pushed 5 times and see 5 instances of one type? Leaked.
- [ ] **Trace every solid outgoing arrow** from a suspect node until it loops back.
- [ ] **Export the graph** before you change code so you have a before/after record.

## How do I export a .memgraph and reopen it offline?

Use **File → Export Memory Graph**, save the `.memgraph` on the remote Mac, then copy it down with `scp` or your SFTP client. You do not have to fix the bug inside the same remote session.

On any Mac with Xcode, double-click the `.memgraph` to reopen it in the debugger. You can also read the same file with the command-line `leaks` tool:

```bash
leaks MyApp.memgraph
leaks MyApp.memgraph | grep -A 10 "PhotoDetailViewController"
```

Check `leaks --help` in your installed Xcode for the flags available in your version; options such as `--fullContent` vary between releases.

## Can I run leaks in CI without a GUI?

Yes. Once you know which type to watch, run `leaks` against the simulator process after your tests boot the simulator and launch the app:

```bash
xcrun simctl spawn booted leaks com.example.MyApp > leaks-report.txt
```

Treat a non-zero exit from `leaks` as a failure and keep the report as a build artifact. You get class names and reference chains in text rather than the visual graph, which makes a good gate once a known cycle is fixed, so it does not quietly return.

## What Mac runs the Memory Graph debugger for your Xcode version?

The [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, $6 a day, 4GB RAM) is the usual fit for Xcode and the Simulator on Apple silicon, and it can run Xcode 26.4 or later. If your app needs Xcode 26.3 or earlier, the [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day, 4GB RAM) also works. Check the macOS version shown before purchase against Apple's Xcode table. VM vCPUs are shared, so time your own run.

**Related guides:**
- [Build DocC Documentation on a Remote Mac](/blog/mac-docc-documentation-remote)
- [Export and Manage Xcode Archives Remotely](/blog/mac-xcode-organizer-archives-remote)
- [Set Up a GitHub Actions Self-Hosted Runner on a Mac](/blog/mac-github-actions-self-hosted-runner)
