Using MariaDB with TCMalloc or jemalloc

You are viewing an old version of this article. View the current version here.

Read the Profiling Memory Usage page for more information on how to debug high memory consumption.

Using tcmalloc or jemalloc

TCMalloc is a malloc replacement library optimized for multi-threaded usage. It also features a built-in heap debugger and profiler.

Another malloc replacement that may speed up MariaDB is jemalloc.

The procedures to use one of these libraries with MariaDB are the same. Many other malloc replacement libraries (as well as heap debuggers and profilers) can be used with MariaDB in a similar fashion.

Building MariaDB with an alternative to malloc

To build MariaDB 5.5 with TCMalloc, you need to use the following command

cmake -DCMAKE_EXE_LINKER_FLAGS='-ltcmalloc'  -DWITH_SAFEMALLOC=OFF

To use jemalloc, the option should be -ljemalloc.

Starting mariadbd-safe with an alternative to malloc

If you want to do this only one time, as a test, you can also start a standard MariaDB server with TCmalloc with:

/usr/sbin/mariadbd-safe --malloc-lib=tcmalloc

If you want to configure mariadbd-safe to use tcmalloc or jemalloc, edit your configuration file, in the [server] or [mariadbd] group:

malloc-lib=tcmalloc

Starting mariadbd with an alternative to malloc

First, locate the library file that needs to be used:

# jemalloc
find /usr/lib -name "libjemalloc.so.*"
# tcmalloc
find /usr/lib -name "libtcmalloc.so.*"

Now pass it to mariadbd using the LD_PRELOAD variable:

LD_PRELOAD=/path/to/library mariadbd

For example, on OpenSuse 15.4 one would do:

Configuring systemd

If you use systemd to run MariaDB, first locate the library as explained above. The locate the service configuration file:

systemctl status mariadb |grep Loaded

Now edit the mariadb.service.d file by adding a line to the [service] group:

Environment=LD_PRELOAD=<path-to-library>

For example:

[service]
Environment=LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2

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

See Also

Comments

Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.