Part 2 of 20 in Tools we built and why. Previous: Why we build our own tools · Next: corewait: asking your CPU why a program is slow
What this is about
When a program needs memory, it asks the computer for a block. When it is done with that block, it should give it back. A memory leak is a block that the program asked for and never gave back. One leak is harmless. A leak inside a loop that runs a million times fills the machine.
heaptide is our tool for finding leaks. You run your program under it, once, without rebuilding the program. At the end you get one report. It says what was never freed, whether memory kept rising, and which lines of code hold it. It works on programs written in C and Mojo, on Python, and on programs that use an AMD graphics card.
Why it matters
A leak does not crash a program at once. A server that leaks a little on every request runs fine for hours, then slows, then dies. A model-serving program that leaks graphics memory can run out of the card's memory long before the computer runs out of normal memory. By the time you see the symptom, the cause is far in the past.
So the useful question is not "is my program using a lot of memory?" It is "is its memory still rising after it has finished starting up?" That second question is what heaptide answers.
How it works
There are four ideas inside heaptide. You do not need to understand them to use it, but they explain why it is fast.
- It listens at the door. Programs get memory by calling a small set of functions, such as
malloc. heaptide is a preload library: a small piece of code that is loaded before your program and hooks those calls. It notes each request and the code that made it. For graphics memory it also hooks the AMD HIP allocation calls. - It keeps a short list. Each live block gets a 16-byte entry in a table. Counts are kept per call site and per thread, so threads do not queue behind one lock.
- It checks at the end. When the program exits, heaptide scans for blocks that are still allocated and that nothing points to any more. That scan follows the design of LeakSanitizer, a well-known leak checker. Blocks are sorted into direct leaks, indirect leaks (only another lost block points to them) and reachable blocks (something still holds them).
- It writes one report. The result goes to a file, and a separate report program turns it into a readable page, with file and line for each site.
The speed comes from doing little at each allocation. Our benchmark runs eight threads, each making one million malloc and free calls on sizes from 16 to 4096 bytes, on a Ryzen 7 7800X3D, with the tools interleaved over eight rounds. The medians were:
| tool | time | memory used by the tool |
|---|---|---|
| heaptide | 0.13 s | 9.3 MB |
| heaptrack 1.5.0 | 1.65 s | 62.1 MB |
| Valgrind massif | 1.97 to 1.99 s | 29 to 33 MB |
On a different, allocation-heavy loop, Valgrind's memcheck was 48 times slower than heaptide. These numbers come from one machine and one benchmark. They show the design is cheap. They do not promise the same ratio on your program.
How to do it, step by step
The project page gives a download and a demo. Here is the short path.
- Download the release tarball for Linux x86-64 from the heaptide GitHub page and unpack it.
- Run your own program under it with
./heaptide run -- COMMAND. - Or try the planted-leak demo that ships with the tool. Build it with
cc -O0 -g -o demo examples/demo.c, run it with./heaptide run --every 50 -- ./demo, then read the report with./heaptide report .work/heaptide --top 3. - Read the report in this order: first the leaks, then the growth verdict, then the holders.
The --every option sets how often heaptide samples memory growth, in milliseconds. Other options include --py, which charges each Python allocation to its source line, and --gpu, which makes each Mojo graphics buffer its own block so a leak cannot hide in a pool.
What it does not do
The limits are written on the project page, and we repeat the main ones here.
- It is for Linux on x86-64 only. There is no native Windows build, though WSL2 works for Linux programs.
- The graphics paths were tested on one machine, with an RX 7900 XTX and ROCm 7.2. Other cards are untried.
- A stray pointer into the middle of a block keeps that block alive, as in LeakSanitizer, so a stray word can hide a leak.
- After a SIGKILL or the out-of-memory killer there is no leak scan. The report says so.
- Graphics and pinned host blocks are not scanned. Any such block still allocated at exit counts as a direct leak.
Words you may not know
- Allocation: a request for a block of memory.
- Free: giving a block back.
- Heap: the pool of memory a program takes blocks from while it runs.
- Memory leak: a block that was never given back.
- Preload library: code loaded ahead of a program so it can watch the program's calls.
- Call site: the line of code that made a request.
- VRAM: the memory on a graphics card.
- SIGKILL: a signal that ends a program at once, with no chance to clean up.
Try it
- Read the heaptide project page for the five questions the report answers.
- Get the code and releases at github.com/amarbarosys/heaptide.
- Run the planted-leak demo first. A tool you have seen find a known leak is easier to believe on an unknown one.
- Check your own server: start it under
heaptide run, send it traffic, stop it, and read the growth line.
Read next
- corewait: asking your CPU why a program is slow: the next tool in the series, about time instead of memory.
- The Mojo libraries: a shelf of small, checked parts: the parts our tools are built from.

Komentarze
Brak komentarzy.
Zaloguj się aby skomentować.