# mem_monitor **Repository Path**: DDL/mem_monitor ## Basic Information - **Project Name**: mem_monitor - **Description**: Monitor memory leak and peak memory snapshot by LD_PRELOAD. - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-18 - **Last Updated**: 2026-01-18 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # mem_monitor mem_monitor is an Android bionic LD_PRELOAD memory/FD leak monitor built for the Soong build system. It tracks malloc/mmap/dmabuf/ION and file descriptor leaks, captures peak memory snapshots, and exports per-process reports in txt and html formats. A separate aggregator tool merges per-process reports into a single summary. ## Features - Android bionic compatible LD_PRELOAD library - Track malloc/calloc/realloc/memalign allocations - Track mmap/munmap allocations (optionally file-backed) - Track file descriptor open/close leaks - Track DMA-BUF allocations via dma-heap ioctl - Track ION allocations via ion ioctl - Peak memory snapshot aggregation by call stack - Periodic report flush without signal-based sampling - Per-process report files + multi-process aggregated report - Configurable stack depth and top-N snapshot entries ## Build (Soong) Copy this project into your AOSP tree and build: ``` mmm path/to/mem_monitor ``` Outputs: - `libmemmon_preload.so` - `memmon_report_aggregator` ## Deploy Push artifacts to the device: ``` adb push out/target/product//system/lib64/libmemmon_preload.so /data/local/tmp/ adb push out/target/product//system/bin/memmon_report_aggregator /data/local/tmp/ ``` (Use lib for 32-bit processes.) ## Usage ### 1) LD_PRELOAD for a target process For a debuggable app: ``` adb shell setprop wrap. "LD_PRELOAD=/data/local/tmp/libmemmon_preload.so" adb shell am force-stop adb shell am start -n / ``` For native services (init rc), add: ``` setenv LD_PRELOAD /data/local/tmp/libmemmon_preload.so ``` ### 2) Configure tracking Use env vars or a config file. Env vars: - `MEMMON_TRACK`: comma list of `malloc,mmap,fd,dmabuf,ion` - `MEMMON_STACK_DEPTH`: call stack depth (default 16) - `MEMMON_REPORT_DIR`: report output directory - `MEMMON_SNAPSHOT_INTERVAL_MS`: periodic flush interval (0 disables) - `MEMMON_PEAK_TOP_N`: top snapshot entries - `MEMMON_MIN_ALLOC`: ignore allocations smaller than this size - `MEMMON_CONFIG`: path to config file Config file example: ``` # /data/local/tmp/memmon.cfg track=malloc,mmap,fd,dmabuf,ion stack_depth=24 report_dir=/data/local/tmp/memmon snapshot_interval_ms=10000 peak_top_n=64 min_alloc=128 ``` ### 3) Collect reports Per-process reports will be created under `MEMMON_REPORT_DIR`: - `_.txt` - `_.html` ### 4) Aggregate multi-process reports ``` adb shell /data/local/tmp/memmon_report_aggregator --input-dir /data/local/tmp/memmon --output-prefix memmon_summary ``` Output: - `/data/local/tmp/memmon/memmon_summary.txt` - `/data/local/tmp/memmon/memmon_summary.html` ## Report contents Each per-process report includes: - Process name - Current bytes and peak bytes - Peak snapshot time - Top-N aggregated snapshot entries with call stacks - Leak allocations with call stacks - Leak file descriptors with call stacks ## ION/DMA-BUF tracking notes - dma-heap allocations are tracked via `DMA_HEAP_IOCTL_ALLOC`. - ION allocations are tracked via `ION_IOC_ALLOC`. - The report associates these allocations with the calling stack. Some vendor ION variants or custom allocators may require extending the ioctl parsing. ## Safety and stability - The sampling path does not use signals to avoid killing the target process. - Reports are flushed periodically when `MEMMON_SNAPSHOT_INTERVAL_MS` is set. - Reports are also flushed at process exit. ## Extension ideas - Add JSON report format for automation - Export to `perfetto` or `trace_marker` for timeline correlation - Track `ashmem` and `memfd` allocations - Add per-allocator sampling mode to reduce overhead - Add symbolization using `unwindstack` for richer stacks ## Notes - LD_PRELOAD requires debuggable apps or appropriate system configuration. - Running as root may be required for system services. - This library is intended for debugging and should not be enabled in production builds.