Using MariaDB with TCMalloc or jemalloc
Using tcmalloc
TCMalloc is a malloc replacement library optimized for multi-threaded usage. It also features a built-in heap debugger and profiler. You can build MariaDB with it or LD_PRELOAD it.
To build MariaDB 5.5 with TCMalloc
, you need to use the following command
cmake -DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc' -DWITH_SAFEMALLOC=OFF
Many other malloc replacement libraries (as well as heap debuggers and profilers) can be used with MariaDB in a similar fashion.
You can also start a standard MariaDB server with TCmalloc
with:
/usr/sbin/mysqld_safe --malloc-lib=tcmalloc
or add it to my.cnf
. Alternatively just set LD_PRELOAD to point to the to be used malloc library:
export LD_PRELOAD=/path/to/library
Using jemalloc
The above procedure also works with jemalloc. Just replace tcmalloc with jemalloc in the above commands.
Finding memory leaks with jemalloc
jemalloc can provide a report of memory leaks at program exit:
MALLOC_CONF=prof_leak:true,lg_prof_sample:0,prof_final:true \ LD_PRELOAD=${JEMALLOC_PATH}/lib/libjemalloc.so.2 path-to-mariadbd
This will produce something like:
<jemalloc>: Leak summary: 267184 bytes, 473 objects, 20 contexts <jemalloc>: Run jeprof on "jeprof.19678.0.f.heap" for leak detail
You can learn more about the memory leaks with jeprof, that is included with jemalloc:
jeprof --show_bytes path-to-mariadbd jeprof.19678.0.f.heap
You can also generate a PDF call graph of the leak:
jeprof --show_bytes --pdf path-to-mariadbd jeprof.19678.0.f.heap > /tmp/mariadbd.pdf