$npx -y skills add dpearson2699/swift-ios-skills --skill ios-memgraph-analysisUse when capturing or analyzing an iOS .memgraph, especially when the task mentions a memory leak, heap growth, persistent memory increase, ownership path, or matched-capture comparison with Apple CLI tools. Covers unambiguous Simulator capture, leaks/heap/vmmap/malloc_history ev
| 1 | # iOS Memgraph Analysis |
| 2 | |
| 3 | Use memory graphs to prove why memory survives a defined lifetime boundary. |
| 4 | Separate unreachable leaks from reachable growth, preserve raw tool output, and |
| 5 | verify the same app-owned type and ownership path after a fix. |
| 6 | |
| 7 | ## Contents |
| 8 | |
| 9 | - [Boundary](#boundary) |
| 10 | - [Evidence Model](#evidence-model) |
| 11 | - [Workflow](#workflow) |
| 12 | - [Ownership Decisions](#ownership-decisions) |
| 13 | - [Common Mistakes](#common-mistakes) |
| 14 | - [Review Checklist](#review-checklist) |
| 15 | - [References](#references) |
| 16 | |
| 17 | ## Boundary |
| 18 | |
| 19 | This skill owns `.memgraph` capture and command-line ownership/growth analysis. |
| 20 | Use the Memory Graph Debugger or Instruments when their interactive graph and |
| 21 | allocation timeline are the primary task. Use source review for a suspected |
| 22 | closure capture only after runtime evidence identifies the lifetime or path. |
| 23 | |
| 24 | ## Evidence Model |
| 25 | |
| 26 | Do not collapse these conditions: |
| 27 | |
| 28 | - **Unreachable leak:** allocated memory no longer has a path from a live root. |
| 29 | An isolated strong cycle can be unreachable and still consume memory. |
| 30 | - **Reachable but abandoned state:** a live root still retains objects the user |
| 31 | flow no longer needs. `leaks` may correctly report zero. |
| 32 | - **Expected cache or pool:** memory survives intentionally and must be judged by |
| 33 | its bound, eviction behavior, and pressure response. |
| 34 | - **Heap regression or fragmentation:** footprint grows because more/larger |
| 35 | allocations persist or dirty pages are poorly utilized, without a leak. |
| 36 | |
| 37 | Apple's leak scanner uses conservative pointer discovery and incomplete type |
| 38 | metadata. Counts can fluctuate, and a zero result does not prove the absence of |
| 39 | an ownership bug. Strong evidence identifies the expected lifetime, an |
| 40 | app-owned type or allocation, and a credible path or isolated reproduction. |
| 41 | |
| 42 | ## Workflow |
| 43 | |
| 44 | ### 1. Define the lifetime before capturing |
| 45 | |
| 46 | Name the object that should disappear and the event that ends its useful life. |
| 47 | For example: `EditorViewModel` should deinitialize after dismissing the editor |
| 48 | and completing pending save work. |
| 49 | |
| 50 | Record one deterministic sequence: |
| 51 | |
| 52 | 1. launch or restore a known state; |
| 53 | 2. take an optional baseline graph; |
| 54 | 3. perform the feature flow; |
| 55 | 4. cross the expected release boundary; |
| 56 | 5. wait for legitimate asynchronous cleanup; |
| 57 | 6. take the post-flow graph. |
| 58 | |
| 59 | Keep build, simulator/device, data, Malloc Stack Logging setting, and repetition |
| 60 | count stable. Malloc Stack Logging adds valuable allocation backtraces but also |
| 61 | overhead; compare only runs with the same setting. |
| 62 | |
| 63 | ### 2. Capture a graph without guessing the process |
| 64 | |
| 65 | Xcode can export a graph from the Memory Graph Debugger. For a running Simulator |
| 66 | app, use the helper from this skill: |
| 67 | |
| 68 | ```bash |
| 69 | mkdir -p /tmp/myapp-memory |
| 70 | mkdir /tmp/myapp-memory/run-01 |
| 71 | python3 scripts/capture_sim_memgraph.py \ |
| 72 | --bundle-id com.example.MyApp \ |
| 73 | --output-dir /tmp/myapp-memory/run-01 \ |
| 74 | --pretty > /tmp/myapp-memory/run-01/capture.json |
| 75 | ``` |
| 76 | |
| 77 | The per-run `mkdir` must fail if the capture directory already exists. Use a |
| 78 | new run name rather than mixing stale evidence with a retry. |
| 79 | |
| 80 | Pass `--udid` when more than one Simulator is booted. The helper accepts only |
| 81 | one exact launchd label and PID; zero or multiple matches are errors. It runs the |
| 82 | host `leaks --outputGraph` command, retains stdout/stderr, and writes a manifest. |
| 83 | Do not replace this with `pgrep | head -1` or a substring match. |
| 84 | |
| 85 | Capturing suspends the process. Do not use capture latency as performance data. |
| 86 | |
| 87 | ### 3. Preserve raw output and build a bounded summary |
| 88 | |
| 89 | ```bash |
| 90 | MEMGRAPH=$(jq -er \ |
| 91 | 'select(.status == "captured") | .memgraph | select(type == "string" and length > 0)' \ |
| 92 | /tmp/myapp-memory/run-01/capture.json) |
| 93 | test -s "$MEMGRAPH" |
| 94 | python3 scripts/summarize_memgraph.py \ |
| 95 | "$MEMGRAPH" \ |
| 96 | --artifact-dir /tmp/myapp-memory/run-01/analysis-raw \ |
| 97 | --app-image 'MyApp|MyFeatureKit' \ |
| 98 | --trace-limit 3 --group-by-type --pretty \ |
| 99 | > /tmp/myapp-memory/run-01/analysis.json |
| 100 | ``` |
| 101 | |
| 102 | Read the exact graph path from the preserved capture report; do not guess a |
| 103 | timestamped filename. The helper creates a dedicated raw-artifact directory, |
| 104 | refuses to reuse it, runs `leaks --list`, and parses only a conservative subset |
| 105 | of its text. `--app-image` marks candidate rows; it does not prove ownership. |
| 106 | `--trace-limit` runs bounded |
| 107 | `leaks --traceTree=<address>` queries. Add `--reference-tree` when aggregate |
| 108 | root paths are more useful than individual leaked addresses. With |
| 109 | `--group-by-type`, that reference-tree query is grouped in the same invocation. |
| 110 | Exit statuses 0 and 1 from ` |