brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: leaks can check a program for lost memory the moment it exits on a Mac

Memory
~/tips/tip-memory-mac-leaks-atexit

leaks -atExit -- ./myprogram --some-option

Test your own command line program for leaked allocations.

If you write C or Objective-C on a Mac, or build someone else’s tool from source, there is a leak checker already installed with the developer tools.

leaks -atExit -- ./myprogram --some-option

It starts the program and inspects its memory when it is about to exit. The report lists blocks that were allocated and can no longer be reached by any pointer, with their sizes. According to the man page, stack traces come along because the option turns on a light form of allocation logging.

The -quiet flag leaves out the header about the process and the loaded libraries, which keeps the output short:

leaks -quiet -atExit -- ./myprogram

A running process can be checked by its ID instead:

leaks 1234

I like the exit check for small programs, since it needs no changes to the code.

It only finds memory that is truly lost. A cache that grows forever still has pointers to it and is reported as in use, so a clean result does not prove the program stays small. It can also be refused for processes you do not own and for system programs that are protected by macOS. Test your own builds, and expect a few oddities from system frameworks that are outside your control.

Further reading: Apple: Finding memory leaks

More Memory tips lsmem --summary for f in /proc/[0-9]*/status; do sudo apt install smem sudo pmap -x 1234 sudo systemctl edit indexer.service All Memory tips (12) All topics

Link