« All posts

Tracking Down a Silent Memory Leak in a Rust macOS NVR

How a Rust macOS NVR's hidden memory leak was isolated using custom malloc zones and traced to a wgpu error path.

The developer of Murlet, an Electron-and-Rust macOS network video recorder that must run unattended for weeks, set out to confirm the app had no memory leaks—and found one. The leak involved data structures that grew slowly but remained reachable, making it invisible to tools like macOS's leaks command or LeakSanitizer.

To isolate the problem, they replaced Rust's global allocator with a thin wrapper over libmalloc's zone API, routing all Rust allocations into a dedicated, named memory zone separate from Electron's PartitionAlloc-backed heap. This let vmmap report the Rust heap independently and made it inspectable with standard tools like heap and malloc_history.

They also built an in-app actor that periodically collects and logs whole-task memory stats, per-tag VM region census data, and per-zone malloc statistics—effectively a programmatic version of Activity Monitor and vmmap—so memory usage could be graphed over time during soak testing. This instrumentation eventually traced the leak to wgpu, triggered only along a repeated error path, and already fixed upstream.

For engineers building long-running native Electron plugins or embedding Rust in larger processes, this is a concrete blueprint for separating allocator provenance and catching slow, reachable memory growth that conventional leak detectors miss.

This synthesis was produced from its source by AI; there is no human editor or manual review step. How we work