Mobile Development 11 min read

How OOMDetector Solves iOS Out‑of‑Memory Crashes and Memory Leaks

This article introduces Tencent's OOMDetector, an iOS memory‑monitoring component that records allocation stacks, detects leaks, and uses optimized hooking, stack compression, and mmap‑based dumping to provide low‑overhead, on‑device analysis of out‑of‑memory crashes and memory‑leak issues.

Tencent TDS Service
Tencent TDS Service
Tencent TDS Service
How OOMDetector Solves iOS Out‑of‑Memory Crashes and Memory Leaks

Component Introduction

In iOS apps there are two dreaded crashes: abnormal exits and out‑of‑memory (OOM) kills. While tools exist for the former, OOM crashes lack stack traces. OOMDetector, a Tencent‑developed iOS memory‑monitoring component, records allocation stacks and detects leaks with minimal overhead.

OOMDetector provides two main functions: 1) OOM stack statistics – records allocation stacks and sizes, dumping them on OOM; 2) Memory‑leak detection for malloc blocks and Objective‑C objects.

Background

Existing iOS memory analysis tools such as Allocation and FBAllocationTracker have limitations: Allocation requires a Mac and degrades performance; FBAllocationTracker only tracks Objective‑C objects and lacks stack information. OOMDetector hooks system allocation methods to capture full stack data and runs independently on the device.

Allocation

Apple’s Allocation tool offers comprehensive monitoring but cannot run on‑device and impacts performance, making it unsuitable for production monitoring.

FBAllocationTracker

FBAllocationTracker uses method swizzling to replace alloc, allowing on‑device monitoring with low impact, but it only tracks OC objects and provides no allocation stack information.

Component Principle

OOM Stack Monitoring

By hooking malloc_zone and vm_allocate, OOMDetector records allocation stacks, counts, and sizes, dumping them to disk when memory pressure occurs.

Performance Challenges

Hooking high‑frequency allocation functions can cause stalls. OOMDetector optimizes stack back‑tracing (using a lightweight address‑only method and server‑side symbol resolution) and lock contention (using OSSpinLock / os_unfair_lock) to keep overhead below 1 µs.

Stack Clustering and Compression

Stacks are hashed with MD5 and clustered; only stacks whose cumulative memory exceeds a threshold retain full data, reducing memory usage to about 1/40 of the original.

Data Dump Strategy

Instead of frequent I/O, OOMDetector writes data via mmap, which maps a file into memory, avoiding copy overhead and ensuring data is flushed when memory is low, the process crashes, or msync is called.

Memory Leak Detection

OOMDetector scans heap, stack, global data, and registers for pointers to allocated blocks; blocks without any pointers are reported as leaks. The scan pauses all threads for 1–2 seconds and is intended for testing phases.

Outlook

The component is open‑sourced on GitHub and will continue to evolve; developers facing iOS memory issues are encouraged to try it.

Performance optimizationiOSMemory Monitoringmemory leak detectionOOM detection
Tencent TDS Service
Written by

Tencent TDS Service

TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.