Junior — Middle
What are the types of memory leaks and how to detect them?
sobes.tech AI
Answer from AI
In iOS, there are several types of memory leaks:
- Retain cycles (cyclic references): when two objects hold strong references to each other, preventing their deallocation.
- Leaks due to improper use of delegates: if a delegate is declared as a strong reference, it can lead to a cycle.
- Leaks due to unreleased resources: for example, timers, notification observers that were not removed.
To detect leaks, Xcode tools are used:
- Instruments (Leaks and Allocations) — allows tracking objects that are not released.
- Memory Graph Debugger — visualizes connections between objects and helps find cycles.
Example: if a ViewController class has a strong reference to a timer that does not stop or nullify, it can lead to a leak.