home / skills / hoangnguyen0403 / agent-skills-standard / memory-management
This skill helps you apply Swift memory management best practices, reinforcing ARC fundamentals, capture lists, and avoiding retain cycles across code.
npx playbooks add skill hoangnguyen0403/agent-skills-standard --skill memory-managementReview the files below or copy the command above to add this skill to your agents.
---
name: Swift Memory Management
description: Standards for ARC, Weak/Unowned References, and Capture Lists
metadata:
labels: [swift, memory, arc, weak, unowned]
triggers:
files: ['**/*.swift']
keywords: ['weak', 'unowned', 'capture', 'deinit', 'retain']
---
# Swift Memory Management
## **Priority: P0**
## Implementation Guidelines
### ARC Fundamentals
- **Default**: Strong references. Swift automatically manages retain/release.
- **Weak**: Use `weak` for delegate patterns and parent-child relationships.
- **Unowned**: Use `unowned` only when reference guaranteed to outlive (rare).
### Capture Lists
- **Closures**: Always use `[weak self]` or `[unowned self]` in escaping closures.
- **Self in Structs**: No capture list needed (`self` is copied by value).
- **Multiple Captures**: `[weak self, weak delegate]`.
### Retain Cycles
- **Delegates**: Always `weak var delegate`.
- **Closures as Properties**: Use `weak` or `unowned` in capture list.
- **two-way References**: One side must be `weak`.
## Anti-Patterns
- **Strong Delegates**: `**No strong var delegate**: Use weak.`
- **Missing Capture List**: `**No self in escaping closures**: Use [weak self].`
- **Unowned Misuse**: `**Avoid unowned**: Use weak unless certain.`
## References
- [Capture Lists & Retain Cycles](references/implementation.md)
This skill codifies Swift memory management standards focused on ARC, weak/unowned references, and closure capture lists. It provides clear guidelines to avoid retain cycles and common anti-patterns for delegates, closures, and two-way relationships. The goal is safer, predictable memory usage in Swift codebases.
The skill inspects code patterns and recommends appropriate reference qualifiers (strong, weak, unowned) and capture lists for closures. It flags delegate properties, closure properties, and bidirectional references, suggesting weak or unowned usage where appropriate. It also highlights risky anti-patterns like strong delegates, missing capture lists in escaping closures, and improper unowned use.
When should I use unowned instead of weak?
Use unowned only when you can guarantee the referenced object outlives the reference; otherwise prefer weak to avoid crashes from dangling pointers.
Do I need capture lists for non-escaping closures?
No. Non-escaping closures don’t create long-lived captures, so capture lists are unnecessary for value types and short-lived closures.