打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Redis采用不同内存分配器碎片率对比

我们知道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 http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gztar zxvf libunwind-0.99-alpha.tar.gzcd libunwind-0.99-alpha/CFLAGS=-fPIC ./configuremake CFLAGS=-fPICmake CFLAGS=-fPIC install

然后再进行google-preftools的安装:

wget http://google-perftools.googlecode.com/files/google-perftools-1.8.1.tar.gztar 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 install  sudo echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf  #如果没有这个文件,自己建一个sudo /sbin/ldconfig

然后再进行Redis的安装,在make时指定相应的参数以启用tcmalloc

$ curl -O http://redis.googlecode.com/files/redis-2.4.4.tar.gz$ 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.7
used_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.1
used_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.7
used_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.1
used_memory:771963304used_memory_human:736.20Mused_memory_rss:800583680used_memory_peak:772784056used_memory_peak_human:736.98Mmem_fragmentation_ratio:1.04mem_allocator:libc

可以看出,在分配大块内存和小块内存上,几种分配器的碎片率差距还是比较大的,大家在使用Redis的时候,还是尽量用自己真实的数据去做测试,以选择最适合自己数据的分配器。

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
redis源码分析(1)内存管理
redis info命令详解
高性能NOSQL数据库redis结合谷歌开源tcmalloc库的安装笔记
为什么Redis的数据被删除,内存占用还这么大?
使用tcmalloc替换系统的malloc
LRB · Jim Holt · Smarter, Happier, More Produ...
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服