我们知道redis并没有自己实现内存池,没有在标准的系统内存分配器上再加上自己的东西。所以系统内存分配器的性能及碎片率会对red
我们知道redis并没有自己实现内存池,没有在标准的系统内存分配器上再加上自己的东西。所以系统内存分配器的性能及碎片率会对redis造成一些性能上的影响。
在redis的 zmalloc.c 源码中,我们可以看到如下代码:
48 /* explicitly override malloc/free etc when using tcmalloc. */ 49 #if defined(use_tcmalloc) 50 #define malloc(size) tc_malloc(size) 51 #define calloc(count,size) tc_calloc(count,size) 52 #define realloc(ptr,size) tc_realloc(ptr,size) 53 #define free(ptr) tc_free(ptr) 54 #elif defined(use_jemalloc) 55 #define malloc(size) je_malloc(size) 56 #define calloc(count,size) je_calloc(count,size) 57 #define realloc(ptr,size) je_realloc(ptr,size) 58 #define free(ptr) je_free(ptr) 59 #endif从上面的代码中我们可以看到,redis在编译时,会先判断是否使用tcmalloc,如果是,会用tcmalloc对应的函数替换掉标准的libc中的函数实现。其次会判断jemalloc是否使得,最后如果都没有使用才会用标准的libc中的内存管理函数。
而在最新的2.4.4版本中,jemalloc已经作为源码包的一部分包含在源码包中,所以可以直接被使用。而如果你要使用tcmalloc的话,是需要自己安装的。
下面简单说一下如何安装tcmalloc包,tcmalloc是google-proftools中的一部分,所以我们实际上需要安装google-proftools。如果你是在64位机器上进行安装,需要先安装其依赖的libunwind库。
wget tar zxvf libunwind-0.99-alpha.tar.gzcd libunwind-0.99-alpha/cflags=-fpic ./configuremake cflags=-fpicmake cflags=-fpic install然后再进行google-preftools的安装:
wget tar zxvf google-perftools-1.8.1.tar.gzcd google-perftools-1.8.1/./configure --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimalmake && make installsudo echo /usr/local/lib > /etc/ld.so.conf.d/usr_local_lib.conf #如果没有这个文件,自己建一个sudo /sbin/ldconfig然后再进行redis的安装,在make时指定相应的参数以启用tcmalloc
$ curl -o $ tar xzvf redis-2.4.4.tar.gz$ cd redis-2.4.4$ make use_tcmalloc=yes force_libc_malloc=yes$ sudo make install再启动redis后通过info命令就能看到使用的内存分配器了。
下面回到本文的主题,对于tcmalloc,jemalloc和libc对应的三个内存分配器。其性能和碎片率如何呢?下面是一个简单测试结果,使用redis自带的redis-benchmark写入等量数据进行测试,数据摘自采用不同分配器时redis info信息。我们可以看到,采用tcmalloc时碎片率是最低的,为1.01,,jemalloc为1.02,而libc的分配器碎片率为1.31,如下所未:
used_memory:708391440used_menory_human:675.57mused_memory_rss:715169792used_memory_peak:708814040used_memory_peak_human:675.98mmem_fragmentation_ratio:1.01mem_allocator:tcmalloc-1.7used_memory:708381168used_menory_human:675.56mused_memory_rss:723587072used_memory_peak:708803768used_memory_peak_human:675.97mmem_fragmentation_ratio:1.02mem_allocator:jemalloc-2.2.1used_memory:869000400used_menory_human:828.74mused_memory_rss:1136689152used_memory_peak:868992208used_memory_peak_human:828.74mmem_fragmentation_ratio:1.31mem_allocator:libc上面的测试数据都是小数据,也就是说单条数据并不大,下面我们尝试设置benchmark的-d参数,将value值调整为1k大小,测试结果发生了一些变化:
used_memory:830573680used_memory_human:792.10mused_memory_rss:849068032used_memory_peak:831436048used_memory_peak_human:792.92mmem_fragmentation_ratio:1.02mem_allocator:tcmalloc-1.7used_memory:915911024used_memory_human:873.48mused_memory_rss:927047680used_memory_peak:916773392used_memory_peak_human:874.30mmem_fragmentation_ratio:1.01mem_allocator:jemalloc-2.2.1used_memory:771963304used_memory_human:736.20mused_memory_rss:800583680used_memory_peak:772784056used_memory_peak_human:736.98mmem_fragmentation_ratio:1.04mem_allocator:libc可以看出,在分配大块内存和小块内存上,几种分配器的碎片率差距还是比较大的,大家在使用redis的时候,还是尽量用自己真实的数据去做测试,以选择最适合自己数据的分配器。