Question 4
Memory growth and OOMs
The app doesn’t obviously leak, but memory rises until the process is killed. How do you track down the cause?
Follow-ups
- Leaks vs high but valid usage?
- Which tools and patterns help most?
Answer outline
Reproduce the problem first. Use the Allocations instrument. Tap Mark Generation before and after navigation loops. Then categorize the cause:
- 1.Retain cycles / true leaks — closures capturing
self, delegate strong refs,NotificationCenteror KVO observers not removed. Find them with Leaks instrument + Memory Graph reference chains. - 2.Valid but unbounded growth — unbounded arrays of full models, duplicate decodes of the same blob, global singletons holding screen data, video buffers. Each needs a policy: eviction, weak refs, streaming, or pagination.
- 3.VM footprint pressure — Instruments Memory report and Xcode memory gauge show overall pressure.
Principles
- Prove retention with Memory Graph paths to root — don’t guess from heap size alone.
- Cap caches; purge on memory warning; test low-memory paths.