memleak(8)
๐ memleak(8) ๐
System Manager's Manual, page memleak(8) โจ
๐ NAME ๐
okay so memleak is the tool that prints a summary of outstanding allocations and their call stacks so you can catch memory leaks ๐ it's Linux eBPF/bcc powered, obviously ๐
๐ฆ SYNOPSIS ๐ฆ
memleak [-h] [-p PID] [-t] [-a] [-o OLDER] [-c COMMAND] [--combined-only] [--wa-missing-free] [-s SAMPLE_RATE] [-T TOP] [-z MIN_SIZE] [-Z MAX_SIZE] [-O OBJ] [INTERVAL] [COUNT]
๐ธ DESCRIPTION ๐ธ
bestie, memleak literally traces and matches memory allocation and deallocation requests ๐ง and collects call stacks for every allocation. then it can show you which call stacks made allocations that never got freed. the main character of leaks ๐ญ
When tracing a specific process, memleak instruments a list of libc allocation functions, specifically: malloc, calloc, realloc, posix_memalign, valloc, memalign, pvalloc, aligned_alloc, and free. When tracing all processes, memleak instruments kmalloc/kfree, kmem_cache_alloc/kmem_cache_free, and also page allocations made by get_free_pages/free_pages. ๐ซถ
โ ๏ธ memleak may introduce significant overhead when tracing processes that allocate and free many blocks very quickly. it's giving slowdown energy ๐ see the OVERHEAD section below, no cap.
This tool only works on Linux 4.6+ ๐ง Stack traces are obtained using the new BPF_STACK_TRACE APIs. For kernels older than 4.6, check the version under tools/old. Kernel memory allocations are intercepted through tracepoints, which are available on Linux 4.7+ โจ
๐ง REQUIREMENTS ๐ง
you'll need CONFIG_BPF and bcc ๐ง non-negotiable, periodt.
๐ OPTIONS ๐
-hโ print the usage message ๐-p PIDโ trace this process ID only (filtered in-kernel) ๐ฏ this traces the libc allocator-tโ print a trace of all allocation and free requests and their results ๐-aโ print a list of allocations that weren't freed (plus their sizes) alongside their call stacks ๐-o OLDERโ only show allocations older thanOLDERmilliseconds. super useful for clearing out false positives ๐ the default is500milliseconds-c COMMANDโ run the specified command and trace only its allocations ๐โโ๏ธ this traces the libc allocator too--combined-onlyโ use statistics precalculated in kernel space. way less data gets pulled from the kernel โก the tradeoff is you lose the time-based false positive filtering (-o) ๐ญ--wa-missing-freeโ make up the action of free to ease misjudgments when free is missing ๐ฉน-s SAMPLE_RATEโ record roughly everySAMPLE_RATE-th allocation to cut down on overhead ๐ฆ-t TOPโ print only the topTOPstacks, sorted by size ๐ the default is10-z MIN_SIZEโ capture only allocations larger than or equal toMIN_SIZEbytes ๐-Z MAX_SIZEโ capture only allocations smaller than or equal toMAX_SIZEbytes ๐-O OBJโ attach to allocation functions in the specified object instead of resolving libc ๐ฎ ignored when kernel allocations are being profiledINTERVALโ print a summary of outstanding allocations and their call stacks everyINTERVALseconds โฐ the default interval is5secondsCOUNTโ print the outstanding allocations summaryCOUNTtimes, then exit ๐
๐ EXAMPLES ๐
ok so here are the vibes, bestie ๐
Print outstanding kernel allocation stacks every 3 seconds:
# memleak 3
Print user outstanding allocation stacks and allocation details for the process 1005:
# memleak -p 1005 -a
Sample roughly every 5th allocation (~20%) of the call stacks and print the top 5 stacks 10 times before quitting:
# memleak -s 5 --top=5 10
Run ./allocs and print outstanding allocation stacks for that process:
# memleak -c ./allocs
Capture only allocations between 16 and 32 bytes in size:
# memleak -z 16 -Z 32
๐ OVERHEAD ๐
memleak can have significant overhead if the target process or kernel performs allocations at a very high rate ๐ญ pathological cases may exhibit up to 100x degradation in running time. most of the time though, memleak shouldn't cause a big slowdown, lowkey relief ๐ you can use the -s switch to reduce the overhead further by capturing only every N-th allocation. the -z and -Z switches can also cut overhead by only capturing allocations of specific sizes ๐ฏ
option --combined-only saves processing time by reusing allocation statistics the kernel already calculated. it's faster, but it lacks info about particular allocations ๐๏ธ
also, option --wa-missing-free makes memleak more accurate in complicated environments ๐ง
to figure out how often your app is calling malloc/free, or how often your kernel is calling kmalloc/kfree, place a probe with perf and collect statistics. for example, to see how many calls to __kmalloc happen in a typical 10 second period:
# perf probe '__kmalloc'
# perf stat -a -e 'probe:__kmalloc' -- sleep 10
๐ฆข SOURCE ๐ฆข
this is from bcc ๐ซถ
https://github.com/iovisor/bcc
also look in the bcc distribution for a companion _examples.txt file with example usage, output, and commentary for this tool ๐
๐ฅ๏ธ OS ๐ฅ๏ธ
Linux ๐ง
๐ STABILITY ๐
โ ๏ธ Unstable โ still in development, so expect some chaos ๐ช๏ธ
๐ AUTHOR ๐
Sasha Goldshtein ๐ฉโ๐ปโจ
bestie, that's the whole page ๐ it's from the USER COMMANDS section, dated 2016-01-14, so it's a little vintage but still iconic ๐ ๐ธ stay curious, find your leaks, and don't let those allocations sneak off unfreed ๐๐ฆ
๐ฆ where this page comes from
- debian bookworm: bpfcc-tools 0.26.0+ds-1 ยท Apache-2.0
- debian trixie: bpfcc-tools 0.31.0+ds-7 ยท Apache-2.0
- ubuntu jammy: bpfcc-tools 0.18.0+ds-2 ยท Apache-2.0
- ubuntu noble: bpfcc-tools 0.29.1+ds-1ubuntu7 ยท Apache-2.0
- ubuntu questing: bpfcc-tools 0.31.0+ds-7ubuntu2 ยท Apache-2.0
- ubuntu resolute: bpfcc-tools 0.35.0+ds-1ubuntu2 ยท Apache-2.0
- ubuntu stonking: bpfcc-tools 0.35.0+ds-1ubuntu4 ยท Apache-2.0
- ๐ girlified by anthropic/claude-haiku-5.5 on : a language model's rewrite of the original, under its terms