Mobile Development 13 min read

Baidu APP Large Memory Allocation Monitoring Solution for iOS

Baidu’s iOS solution monitors single memory allocations exceeding an 8 MB threshold by intercepting malloc_logger and capturing stack traces with backtrace, merging online user‑scenario data and offline pipelines to identify large‑allocation hotspots, reduce OOM crashes, and give developers actionable debugging insight.

Baidu Geek Talk
Baidu Geek Talk
Baidu Geek Talk
Baidu APP Large Memory Allocation Monitoring Solution for iOS

APP crashes caused by insufficient memory are commonly referred to as OOM (Out Of Memory) errors. On iOS, OOM exceptions cannot be captured, and no stack trace information is available, making troubleshooting extremely challenging. The root cause of OOM is unreasonable memory allocation, particularly when the Jetsam mechanism is triggered by single large memory allocations exceeding the threshold when memory is at dangerous levels. Based on real-world data monitoring, Baidu APP has numerous cases where single memory allocations exceed 30MB.

To address this potential OOM risk, Baidu developed a large memory allocation monitoring solution that leverages both online monitoring advantages (rich real user scenarios and user paths) and offline pipeline advantages (can obtain more stack information). The solution combines online monitoring and offline pipeline monitoring to achieve comprehensive large memory monitoring for Baidu APP.

The large memory monitoring consists of two essential functional modules: obtaining memory allocation details and obtaining stack trace information. The first module determines whether a single memory allocation exceeds the threshold (typically 8MB for iOS), identifying large memory allocation behaviors. The second module provides rich stack information to help developers locate the specific code causing large memory allocations.

Baidu's technical approach involves resetting the malloc_logger function pointer in libsystem_malloc to capture memory activity details (allocation and deallocation). By filtering through the Type parameter, memory allocation activities can be identified. This approach can monitor both Objective-C objects and C/C++ objects without invasive modifications to the iOS framework.

For stack trace acquisition, Baidu uses the dyld library to generate address ranges for all APP libraries, then uses backtrace to obtain stack addresses in a dedicated background thread. The client compares stack function addresses and formats crash-like logs, while the server performs symbol resolution using atos and dsym files. This approach avoids performance issues - backtrace with depth 20 takes only 3ms on high-end devices.

The solution has achieved three objectives: reducing OOM rates through memory optimization, providing data visibility into large memory allocation scenarios across Baidu APP, and serving as a preventive mechanism encouraging developers to adopt appropriate memory allocation practices.

Performance OptimizationiOSMemory Managementdyldoommemory-monitoringbacktracemalloc_logger
Baidu Geek Talk
Written by

Baidu Geek Talk

Follow us to discover more Baidu tech insights.

0 followers
Reader feedback

How this landed with the community

login 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.