码迷,mamicode.com
首页 > 其他好文 > 详细

说下Redis采用不同内存分配器

时间:2016-03-01 22:46:59      阅读:724      评论:0      收藏:0      [点我收藏+]

标签:

参考文章: http://blog.sina.com.cn/s/blog_51df3eae01016peu.html

 

我们知道Redis并没有自己实现内存池,没有在标准的系统内存分配器上再加上自己的东西。所以系统内存分配器的性能及碎片率会对Redis造成一些性能上的影响。

在Redis的 zmalloc.c 源码中,我们可以看到如下代码:

 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中的内存管理函数。

 

参考里已经列出了安装tcmalloc的安装方法,下面我说下jemalloc的安装方法

tar jxf jemalloc-3.6.0.tar.bz2
cd jemalloc-3.6.0
 
./configure
make USE_JEMALLOC=yes FORCE_LIBC_MALLOC=yes      #指定用jemalloc
make install
cp -a /root/jemalloc-3.6.0/include/jemalloc/* /usr/include/ cd .. cat /etc/ld.so.conf.d/local_lib.conf echo "/usr/local/lib" > /etc/ld.so.conf.d/local_lib.conf /sbin/ldconfig

 

#至于是选择tcmalloc还是jemalloc,看个人喜好了

ptmalloc 是glibc的内存分配管理模块

tcmalloc   是Google的内存分配管理模块

jemalloc   是BSD的内存分配管理模块

 

说下Redis采用不同内存分配器

标签:

原文地址:http://www.cnblogs.com/nika86/p/5232540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!