iOS Retain Cycle and Memory Leak Diagnostic Framework
Construct a systematic diagnostic framework to isolate and resolve ARC memory leaks and unbounded allocations.
Use this template when debugging persistent memory spikes, unreleased view controllers, or native CoreFoundation leaks in iOS applications. It establishes a repeatable workflow for engineering teams using Xcode Instruments.
Role: Staff Apple Performance & Memory Diagnostics Engineer specializing in Automatic Reference Counting (ARC) and Instruments profiling.
Context
- Target Module or Feature: {{app_module}}
- Memory Profile Data: {{instruments_trace_data}}
- Lifecycle Architecture: {{lifecycle_type}}
- Memory Allocation Ceiling: {{peak_memory_threshold_mb}}
- Routing and Navigation Pattern: {{navigation_architecture}}
- External Dependencies: {{third_party_frameworks}}
Task
Deliver an end-to-end memory diagnostic and remediation framework that isolates retain cycles, closure capture bugs, and object deallocation failures within {{app_module}} to keep runtime footprint below {{peak_memory_threshold_mb}}.
Method
- Ingest {{instruments_trace_data}} to categorize allocation anomalies into persistent leaks, transient heap growth, or abandoned memory.
- Trace ownership graphs in {{app_module}} to inspect strong reference chains across delegates, closures, and observation tokens.
- Audit {{navigation_architecture}} transitions to ensure popped or dismissed controllers execute
deinitdeterministically. - Examine background tasks and event streams originated by {{third_party_frameworks}} for uncancelled references.
- Define synthetic reproduction test scripts simulating repetitive push/pop cycles under {{lifecycle_type}} constraints.
- Formulate unit and integration test assertions using weak references to programmatically verify deallocation on teardown.
- Construct a step-by-step remediation guide covering closure capture lists (
[weak self]), delegate declarations, and Core Foundation unmanaged pointers.
Constraints
- MUST identify the root cause category (closure capture, notification observer, delegate retain, or native CF object) for every identified leak.
- MUST NOT rely solely on manual Instruments visual inspection; provide automated assertion strategies.
- MUST specify explicit teardown procedures for {{third_party_frameworks}} bindings.
- Keep recommendations strictly compatible with modern ARC semantics.
Output format
- Section 1: Leak Hierarchy & Ownership Graph (breakdown of leaky paths and owning objects)
- Section 2: Automated Teardown Verification Harness (code pattern for unit test leak assertions)
- Section 3: Engineering Remediation Playbook (step-by-step fix patterns for discovered retain cycles)
- Section 4: CI/CD Memory Gate Specifications (threshold configs to block regressions exceeding {{peak_memory_threshold_mb}})
- Total length: 400-750 words.
Self-review
- Ensure all variables are correctly referenced and applied to the diagnostic logic.
- Verify that both UIKit/SwiftUI lifecycle concerns are accounted for under the specified architecture.
- Confirm the remediation steps explicitly solve retain cycles without creating unsafe unowned references.
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.