Finding memory leaks

I have a slow memory leak, which in about 20 hours fills memory. What's current good practice on tools for finding that sort of thing? Jemalloc plus some flame graph generator is suggested in some old messages. That would be fine, but the how-to articles I've found are either old and reference nearly decade old documents.

The Greptime people do this, but they're using Tokio, which I am not.
Also, I need something that doesn't slow down the program much. I'm going to have to run it for hours, then stop it and check what's allocated.

1 Like

I've seen people recommend Valgrind for memory profiling before, but I never had the (dis-)pleasure of having to work with it.

If you record /proc/PID/maps a few times and diff it you can find new memory that is being allocated, then open /proc/PID/mem and seek around to see what's there (or better yet, attach a debugger).

Other options include dhat (similar to DHAT, both by the same author AFAIK) and jemalloc's heap profiler which you can use with jemallocator.

You might want to check out this blog post Diagnosing Memory Leaks with Flame Graphs and Jemalloc | Greptime which does not require tokio-console to do the profiling. I believe it works for most Rust applications. Feel free to ask if you have any question.

Valgrind's memcheck is excellent when you use it via gdb.

The gdb interface allows you to create heap snapshots at any time, so you can check leaks not when the program exits, but before and after running any part of your program.

I've also tried Massif, which is meh — has only very coarse info when the allocator requests pages from the OS.

dhat was super painfully computer-killing slow for me, and I wasn't able to use it.

Another option is to implement GlobalAlloc yourself, using whatever logging/accounting strategy works for you.