iOS Memory Leak and Retain Cycle Remediation Plan
Construct a systematic diagnosis and remediation plan to eliminate memory leaks, retain cycles, and OOM terminations in iOS apps.
Deploy this template when investigating critical memory pressure crashes, deallocation failures, and background memory terminations in iOS applications. It guides performance leads to trace retain graphs and structure deterministic fixes.
Role: Principal iOS Performance and Debugging Engineer specializing in Memory Graph Analysis and Xcode Instruments diagnostics.
Context
- Target iOS module: {{app_module_name}}
- Metric or crash signal: {{crash_rate_metric}}
- Target screens or user flows: {{affected_screens}}
- Diagnostic traces and tool findings: {{instruments_findings}}
- Target hardware profile: {{reproduction_device_matrix}}
Task
Formulate a rigorous debugging, remediation, and architectural containment plan to eliminate retain cycles, unbounded memory growth, and Jetsam OOM crashes in {{app_module_name}} across {{affected_screens}} based on {{instruments_findings}}.
Method
- Evaluate the provided {{instruments_findings}} to isolate strong reference cycles, lingering closures, and delegate retain patterns.
- Map the object lifecycle graph from initial view controller presentation to dismissal within {{affected_screens}}.
- Identify external observer subscriptions (Combine AnyCancellable bags, NotificationCenter, KVO) failing to unregister.
- Design deterministic memory leak isolation unit tests utilizing weak references and expectation fulfillments.
- Formulate refactoring guidelines replacing implicit
selfcaptures with structured capture lists ([weak self]). - Establish image/asset cache limits and background purge routines tailored to {{reproduction_device_matrix}}.
- Detail a continuous regression prevention pipeline incorporating MemGraph CI assertions to stabilize {{crash_rate_metric}}.
Constraints
- MUST provide actionable code-level fixes rather than generic deallocation advice.
- MUST NOT permit unconstrained memory footprints under background execution transitions.
- All proposed fixes MUST be verifiable via Xcode Memory Graph Debugger or Instruments Leaks template.
- Avoid introducing force unwrap patterns when unwrapping
weak selfcaptures.
Output format
Provide the remediation plan in the following structure:
- Root Cause & Retain Graph Breakdown (max 200 words)
- Step-by-Step Code Remediation Strategy (ordered by priority)
- Automated Regression Verification Plan (unit test & CI memory assertions)
- Target Metric Milestones (measurable recovery targets for {{crash_rate_metric}})
Self-review
- Verify that the plan directly resolves observations in {{instruments_findings}}.
- Confirm that hardware limits for {{reproduction_device_matrix}} are considered in cache sizing.
- Check that weak reference capture rules are explicitly detailed without dangerous force-unwraps.
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.