其实,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
tcmalloc | jemalloc | libc
关于Redis采用不同内存分配器碎片率对比,参看:http://www.linuxidc.com/Linux/2012-06/62161.htm
网上的相关参考:http://www.linuxidc.com/Linux/2012-06/62159.htm
由于网上的很多资料所使用的版本都比较老,这里打算使用各个组件的最新版本重新安装一次。
Step 1. 64位操作系统请先安装libunwind库(32位操作系统不要安装)
http://www.nongnu.org/libunwind/download.html
安装过程如下:
$ wget http://mirror.yongbok.net/nongnu/libunwind/libunwind-1.0.1.tar.gz
$ tar zxvf libunwind-1.0.1.tar.gz
$ cd libunwind-1.0.1/
$ CFLAGS=-fPIC ./configure
$ make CFLAGS=-fPIC
$ make CFLAGS=-fPIC install
Step 2、安装google-perftools:
http://code.google.com/p/gperftools/downloads/list?redir=1
安装过程如下:
$ wget http://gperftools.googlecode.com/files/gperftools-2.0-1.i386.rpm
$ chmod 777 gperftools-2.0-1.i386.rpm
$ rpm -ivh gperftools-2.0-1.i386.rpm
Step 3. 安装Redis
$ wget http://redis.googlecode.com/files/redis-2.4.13.tar.gz$ tar xzf redis-2.4.13.tar.gz
$ cd redis-2.4.13
$ make
启动redis:
$ src/redis-server
启动控制台:
$ src/redis-cli
redis> set foo bar
OK
redis> get foo
"bar"
