Part 13 in Measuring things honestly. Earlier parts include finding the slow part of a program.
What this is about
Your computer has a limited amount of memory, called RAM. Every running program uses some of it. A tool like the task manager shows you a number for each program.
That number is a start, but it is not the whole story. A program can hold a lot of memory on purpose, such as a cache. It can hold memory by mistake, which is called a leak. And it can grow slowly until it runs out. The task manager shows all three as one number.
This article shows how to tell them apart.
Why it matters
Memory problems are slow and quiet. A program works for hours and then is killed because it ate everything. Or a program seems fine and quietly uses ten times what it needs.
If you only look at one number, you cannot say whether to fix a leak, change a setting or buy more RAM.
How it works
Three words to know
- Leak: memory a program asked for and never gave back, and can no longer reach.
- Growth: memory that keeps rising after the program has finished starting up.
- Holder: the part of the code that is keeping memory alive right now.
Step 1: Watch it over time
One snapshot says little. Take a measurement now, and again later. A program that stays flat after start-up is stable. A program that keeps rising is either leaking or collecting data.
Step 2: Name the holder
When memory is large, ask who holds it. Here is a real case.
On our video server, one cache entry kept a 14.74 GB text encoder in RAM between prompts. The cache stores results for prompts it has seen. The entry for a loader node held a reference to the encoder, and that reference kept the encoder alive.
After a shot, RAM was 31.7 GB. When the cache hit, the encoder was never loaded again, and RAM after a shot fell to 3.1 GB. The warm shot dropped from 85 seconds to 49. The output was identical on both paths.
We tried three settings on the same server:
| Setting | Memory | Warm shot |
|---|---|---|
| Default, cache miss | 31.7 GB after a shot | 85 s |
| Cache hit | 3.1 GB after a shot | 49 s |
| Cache switched off | 2.9 GB over five prompts | 61 s (54 to 67) |
The holder had a name. The cause also had a setting: the cache's headroom, the amount it may hold before it throws entries out. At our memory setting of 36 GB, the default headroom is 3.6 GB. That was too small to free the encoder before the next prompt loaded its own. Raising it to 12 GB kept memory flat at 5.0 to 5.1 GB over three prompts. A run with the cache limited to eight entries was killed for lack of memory.
This was not a leak. It was a cache holding memory by design, with a limit set too low. A single memory number cannot show that. A holder can.
Step 3: Do not trust the task manager on a GPU
GPU memory is harder. The ROCm runtime keeps its own pools. A tool that only watches the CPU side can report a fixed block as a leak, or miss a real one.
Using heaptide
Our tool heaptide finds leaks in C, Mojo, HIP (AMD GPU) and Python programs in one run, without rebuilding them. It works as a preload library that watches memory requests, then runs a scan at exit in the manner of LeakSanitizer.
One report answers five questions: what was never freed, whether live memory keeps rising after start-up, who holds memory now, which places allocate most often, and which allocations are freed straight after they are made.
It sorts unfreed blocks into three classes: direct (nothing points to them any more), indirect (only a lost block points to them), and reachable (still held through something the program can reach).
On a benchmark of eight threads doing one million allocations and frees each, heaptide took 0.13 seconds (median) against 0.02 for no tool. heaptrack took 1.65 seconds and Valgrind's memcheck took 5.55 to 5.68. On that allocation-heavy loop heaptide is still about six times slower than no tool at all, and we say so. Its test suite has 239 checks against planted bugs.
Rules
- Measure over time, not once.
- Ask who holds the memory, not only how much.
- Tell a cache from a leak: a cache has a limit and a leak does not.
- Be careful with GPU memory and pools.
- Say how much your measuring tool costs.
Glossary
- RAM: the fast memory a running program uses.
- Leak: memory never given back and no longer reachable.
- Cache: a store of recent results, kept to save time.
- Holder: the code keeping a block alive.
- Headroom: how much memory a cache may hold before it clears entries.
- VRAM: the memory on a graphics card.
- Allocation: a request for memory.
Try it
Open the task manager. Watch one program for ten minutes while you use it. Does its memory fall again, or only rise?
To try a real leak finder, read the heaptide page and the code at github.com/amarbarosys/heaptide. The release needs Linux on x86-64, and ./heaptide run -- CMD... runs a program under it. The cache story is in Cache Holds Fourteen Gigabytes, Encoder.
What to remember
One memory number says how much. Over time and with a named holder, it says why.

Komentarze
Brak komentarzy.
Zaloguj się aby skomentować.