Home » Random Goodies » Changing MySQL memory allocator to TCMalloc

Changing MySQL memory allocator to TCMalloc

For those of you who don’t know TCMalloc is short for Thread-Caching Malloc. It’s docs can be found here which show a more in depth view. TCMalloc can also be used with other applications via LD_PRELOAD, or by recompiling with the -ltcmalloc linker flag. If you plan on using it with a pre-installed application using the LD_PRELOAD option, first make sure the application is linked against libpthread.so! (ldd is your friend)

We’re mainly going to focus on loading it into MySQL, which is being tested on a CentOS 6 x86_64 server. There are two easy ways of installing gperftools-libs:

1. Install EPEL and run:
yum -y install gperftools-libs

2. Download and install only the packages:
+ 32bit: rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/i386/{libunwind-1.1-3.el6.i686.rpm,gperftools-libs-2.0-11.el6.3.i686.rpm}

+ 64bit: rpm -Uhv http://dl.fedoraproject.org/pub/epel/6/x86_64/{gperftools-libs-2.0-11.el6.3.x86_64.rpm,libunwind-1.1-3.el6.x86_64.rpm}

Now you will notice a few new libraries:

root@ill [~]# rpm -ql gperftools-libs
/usr/lib64/libprofiler.so.0
/usr/lib64/libprofiler.so.0.3.0
/usr/lib64/libtcmalloc.so.4
/usr/lib64/libtcmalloc.so.4.1.0
/usr/lib64/libtcmalloc_and_profiler.so.4
/usr/lib64/libtcmalloc_and_profiler.so.4.1.0
/usr/lib64/libtcmalloc_debug.so.4
/usr/lib64/libtcmalloc_debug.so.4.1.0
/usr/lib64/libtcmalloc_minimal.so.4
/usr/lib64/libtcmalloc_minimal.so.4.1.0
/usr/lib64/libtcmalloc_minimal_debug.so.4
/usr/lib64/libtcmalloc_minimal_debug.so.4.1.0
root@ill [~]#

We’re going to be using /usr/lib64/libtcmalloc_minimal.so.4 moving forward. The library can be defined a few different ways as noted here, and we’re going to be adding it directly to my.cnf.

All that’s left, is to open up /etc/my.cnf (or wherever it’s located), and add the following entry under the [mysqld_safe] section:

[mysqld_safe]
malloc-lib=/usr/lib64/libtcmalloc_minimal.so.4

Note, the location of libtcmalloc_minimal.so will be different depending on your architecture – grab the location from the RPM file list output.

Now just restart MySQL, and enjoy the boost!