r/coolgithubprojects • u/itsAtharv-lol • 47m ago
I wrote a heap profiler for Windows: run your unmodified C/C++ program and get leaks with file:line, peak memory and a flame graph in one HTML file
On Linux I'd reach for heaptrack. On Windows I kept ending up with Visual Studio heap snapshots, UMDH, or wrestling ETW traces in WPA, so I wrote my own.
What it does
- No changes to your program.
heapscope run -- app.exestarts it under the profiler,heapscope attach --pid Njoins one that's been running for hours. No rebuild, no admin rights for your own processes. - Sees everything that goes through the Windows heap:
malloc/free,new/delete,HeapAlloc, with the MSVC runtime linked dynamically (/MD) or statically (/MT). - Leak candidates grouped by call path, with source file and line from your PDBs.
- Peak memory and what held it, on a timeline.
- Diff two runs, or two markers inside one run ("what grew between level load and level unload?").
- One self-contained HTML report: summary, call tree, flame graph, timeline and diff. You can attach it to a bug ticket. It makes no network requests (a CSP enforces it).
- Versioned JSON summary (
analyze -o summary.json) for scripts.
Trying it takes about two minutes, because the zip includes a demo that leaks on purpose:
heapscope run -o leak.hstrace -- demo\leak-demo.exe
heapscope analyze leak.hstrace
heapscope report leak.hstrace -o report.htmlCustom allocators and markers
If you use pools or arenas, the heap only sees the big block, so the objects inside are invisible. There's a small MIT-licensed header (so it can go into proprietary code) with a few macros:
#include "heapscope/heapscope.h"
void* Pool::alloc() {
void* p = take_slot();
HEAPSCOPE_ALLOC(p, kSlotSize, this); // report the object
return p;
}
void Pool::free(void* p) {
HEAPSCOPE_FREE(p, this);
give_back(p);
}
void Game::on_level_loaded() {
HEAPSCOPE_MARK("level loaded"); // a named point you can diff against later
}There's nothing to link. The header finds the profiler DLL at run time.
How it works
It injects a DLL into the target and hooks ntdll's RtlAllocateHeap / RtlReAllocateHeap / RtlFreeHeap with MinHook. That's the one place the CRT, new and HeapAlloc all end up, whatever runtime you link. Hooks write raw return addresses into per-thread ring buffers, a background thread flushes them to a compressed trace file, and symbols are resolved afterwards through DbgHelp, so the hot path stays cheap. If your program crashes mid-capture, the trace is still readable. The tool itself is written in Rust.
Limits, before anyone asks
- Overhead: a program that does almost nothing but allocate ran 5-8x slower with a full capture, and about 1.8x with
--sample 10. Normal programs are much less affected. - x64 Windows only, tested with MSVC builds. No 32-bit or ARM64 yet.
VirtualAllocisn't tracked yet, so memory a custom allocator takes straight from the OS needs the header. Child processes aren't followed.- The binaries aren't code-signed yet, so SmartScreen or Smart App Control may complain. The troubleshooting page covers it, and the release lists SHA-256s. Free code signing through the SignPath Foundation is planned.
- Don't use it on online games with anti-cheat. DLL injection looks like tampering.
- The biggest thing I've run it on is an Assimp model import. I honestly don't know how it behaves on your 2-million-line engine, and that's what I'd like to find out.
What I'd love from you
Run it on something real and tell me where it fell over or what was confusing. There's a feedback thread, or just reply here.
(Also working on a separate paid tool that fails CI when memory regresses. It's not out, and the profiler doesn't need it. If that sounds useful, I'd like to hear about it.)