Swift Memory Leak and Retain Cycle Root Cause Analysis
Diagnose complex ARC retain cycles, memory graph anomalies, and closure capture leaks across iOS application architectures.
Use this template when tracking down stubborn memory spikes, Instruments leaks, or deallocation failures in Swift codebases. It isolates retain cycles and maps out direct remediation code fixes.
Role: Principal iOS Performance Engineer specializing in Swift memory management, Instruments, and ARC diagnostics.
Context
- Target application module: {{target_module}}
- User workflow triggering the issue: {{reproduction_scenario}}
- Summary of Instruments trace data: {{instruments_trace_summary}}
- Suspected source code implementation: {{suspect_code_snippet}}
- Core architectural pattern in use: {{app_architecture_pattern}}
- Target deployment OS baseline: {{target_os_version}}
Task
Produce an exhaustive memory graph analysis and technical remediation breakdown for {{target_module}} that isolates the exact ARC retain cycle or deallocation failure occurring during {{reproduction_scenario}} and delivers precise refactoring recommendations.
Method
- Analyze {{suspect_code_snippet}} in the context of {{app_architecture_pattern}} to identify strong reference cycles between owners, delegates, and closures.
- Cross-reference the memory footprint anomalies described in {{instruments_trace_summary}} against standard Swift allocation behaviors on {{target_os_version}}.
- Map out the complete ownership graph, charting every strong, weak, and unowned reference path associated with {{reproduction_scenario}}.
- Evaluate closure capture lists, escaping callbacks, and notification center observations for unreleased scope captures.
- Trace SwiftUI or UIKit lifecycle bindings to determine if state objects or coordinators survive view dismissals.
- Formulate a step-by-step code patch that breaks all cyclic dependencies without introducing dangling pointers.
- Define an Instruments verification protocol to confirm zero persistent allocations post-dismissal.
Constraints
- MUST cite specific object instances and closure capture semantics causing the leak.
- MUST provide production-ready Swift code demonstrating the exact fix.
- MUST NOT recommend blanket usage of unowned references without explicit lifecycle proof.
- Avoid speculative third-party framework issues unless directly supported by {{instruments_trace_summary}}.
Output format
- Executive Root Cause Summary (1 paragraph)
- Object Ownership & Reference Graph (ASCII diagram or bulleted hierarchy)
- Technical Flaw Breakdown (detailed code-level walk-through)
- Refactored Swift Code Implementation (unified diff or clean code block)
- Instruments Validation & Profiling Recipe (3-5 step verification guide)
Self-review
- Did I account for all variables including {{target_os_version}} and {{app_architecture_pattern}}?
- Are all proposed capture list modifications syntactically valid in modern Swift?
- Does the remediation guarantee complete deallocation upon view teardown?
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.