我们知道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的时候,还是尽量用自己真实的数据去做测试,以选择最适合自己数据的分配器。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/1928824.html