Memory allocation debugging

Sometimes it’s beneficial to see the actual memory allocations done by an app, but you don’t want to code a full memory allocation system. Perhaps you cannot even rebuild the app.

Then a good option is to use a dynamic library to override all the allocation functions.

The idea is simple, make sure the application finds your library before any other, and let it use the custom malloc-functions. Here is the source

On MacOS, you’ll use

1
$ DYLD_INSERT_LIBRARIES=libmemprofile.dylib ./a.out

and on Linux:

1
$ LD_PRELOAD=libmemprofile.so ./a.out

The result will look like this:

1
2
3
4
5
6
$ DYLD_INSERT_LIBRARIES=libmemprofile.dylib ./a.out
Memory used:

 Total:  512 bytes in 2 allocation(s)

At exit:  0 bytes in 0 allocation(s)

Mathias Westerdahl

Mathias Westerdahl

A game developer since 2001. Currently at http://www.defold.com. Find me at twitter