我們知道Redis并沒有自己實(shí)現(xiàn)內(nèi)存池,沒有在標(biāo)準(zhǔn)的系統(tǒng)內(nèi)存分配器上再加上自己的東西。所以系統(tǒng)內(nèi)存分配器的性能及碎片率會(huì)對(duì)Redis造成一些性能上的影響。
在Redis的 zmalloc.c 源碼中,我們可以看到如下代碼:
/* Double expansion needed for stringification of macro values. */
#define __xstr(s) __str(s)
#define __str(s) #s
#if defined(USE_TCMALLOC)
#define ZMALLOC_LIB ("tcmalloc-" __xstr(TC_VERSION_MAJOR) "." __xstr(TC_VERSION_MINOR))
#include google/tcmalloc.h>
#if (TC_VERSION_MAJOR == 1 TC_VERSION_MINOR >= 6) || (TC_VERSION_MAJOR > 1)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) tc_malloc_size(p)
#else
#error "Newer version of tcmalloc required"
#endif
#elif defined(USE_JEMALLOC)
#define ZMALLOC_LIB ("jemalloc-" __xstr(JEMALLOC_VERSION_MAJOR) "." __xstr(JEMALLOC_VERSION_MINOR) "." __xstr(JEMALLOC_VERSION_BUGFIX))
#include jemalloc/jemalloc.h>
#if (JEMALLOC_VERSION_MAJOR == 2 JEMALLOC_VERSION_MINOR >= 1) || (JEMALLOC_VERSION_MAJOR > 2)
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) je_malloc_usable_size(p)
#else
#error "Newer version of jemalloc required"
#endif
#elif defined(__APPLE__)
#include malloc/malloc.h>
#define HAVE_MALLOC_SIZE 1
#define zmalloc_size(p) malloc_size(p)
#endif
#ifndef ZMALLOC_LIB
#define ZMALLOC_LIB "libc"
#endif
從上面的代碼中我們可以看到,Redis在編譯時(shí),會(huì)先判斷是否使用tcmalloc,如果是,會(huì)用tcmalloc對(duì)應(yīng)的函數(shù)替換掉標(biāo)準(zhǔn)的libc中的函數(shù)實(shí)現(xiàn)。其次會(huì)判斷jemalloc是否使得,最后如果都沒有使用才會(huì)用標(biāo)準(zhǔn)的libc中的內(nèi)存管理函數(shù)。
而在最新的2.4.4版本中,jemalloc已經(jīng)作為源碼包的一部分包含在源碼包中,所以可以直接被使用。而如果你要使用tcmalloc的話,是需要自己安裝的。
下面簡(jiǎn)單說一下如何安裝tcmalloc包,tcmalloc是google-proftools中的一部分,所以我們實(shí)際上需要安裝google-proftools。如果你是在64位機(jī)器上進(jìn)行安裝,需要先安裝其依賴的libunwind庫(kù)。
wget http://download.savannah.gnu.org/releases/libunwind/libunwind-0.99-alpha.tar.gz tar zxvf libunwind-0.99-alpha.tar.gz cd libunwind-0.99-alpha/ CFLAGS=-fPIC ./configure make CFLAGS=-fPIC make CFLAGS=-fPIC install
然后再進(jìn)行g(shù)oogle-preftools的安裝:
wget http://google-perftools.googlecode.com/files/google-perftools-1.8.1.tar.gz tar zxvf google-perftools-1.8.1.tar.gz cd google-perftools-1.8.1/ ./configure --disable-cpu-profiler --disable-heap-profiler --disable-heap-checker --disable-debugalloc --enable-minimal make make install sudo echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf #如果沒有這個(gè)文件,自己建一個(gè) sudo /sbin/ldconfig
然后再進(jìn)行Redis的安裝,在make時(shí)指定相應(yīng)的參數(shù)以啟用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
再啟動(dòng)Redis后通過info命令就能看到使用的內(nèi)存分配器了。
下面回到本文的主題,對(duì)于tcmalloc,jemalloc和libc對(duì)應(yīng)的三個(gè)內(nèi)存分配器。其性能和碎片率如何呢?
下面是一個(gè)簡(jiǎn)單測(cè)試結(jié)果,使用Redis自帶的redis-benchmark寫入等量數(shù)據(jù)進(jìn)行測(cè)試,數(shù)據(jù)摘自采用不同分配器時(shí)Redis info信息。
我們可以看到,采用tcmalloc時(shí)碎片率是最低的,為1.01,jemalloc為1.02,而libc的分配器碎片率為1.31,如下所未:
used_memory:708391440 used_menory_human:675.57M used_memory_rss:715169792 used_memory_peak:708814040 used_memory_peak_human:675.98M mem_fragmentation_ratio:1.01mem_allocator:tcmalloc-1.7
used_memory:708381168 used_menory_human:675.56M used_memory_rss:723587072 used_memory_peak:708803768 used_memory_peak_human:675.97M mem_fragmentation_ratio:1.02mem_allocator:jemalloc-2.2.1
used_memory:869000400 used_menory_human:828.74M used_memory_rss:1136689152 used_memory_peak:868992208 used_memory_peak_human:828.74M mem_fragmentation_ratio:1.31mem_allocator:libc
上面的測(cè)試數(shù)據(jù)都是小數(shù)據(jù),也就是說單條數(shù)據(jù)并不大,下面我們嘗試設(shè)置benchmark的-d參數(shù),將value值調(diào)整為1k大小,測(cè)試結(jié)果發(fā)生了一些變化:
used_memory:830573680 used_memory_human:792.10M used_memory_rss:849068032 used_memory_peak:831436048 used_memory_peak_human:792.92M mem_fragmentation_ratio:1.02mem_allocator:tcmalloc-1.7
used_memory:915911024 used_memory_human:873.48M used_memory_rss:927047680 used_memory_peak:916773392 used_memory_peak_human:874.30M mem_fragmentation_ratio:1.01mem_allocator:jemalloc-2.2.1
used_memory:771963304 used_memory_human:736.20M used_memory_rss:800583680 used_memory_peak:772784056 used_memory_peak_human:736.98M mem_fragmentation_ratio:1.04mem_allocator:libc
可以看出,在分配大塊內(nèi)存和小塊內(nèi)存上,幾種分配器的碎片率差距還是比較大的,大家在使用Redis的時(shí)候,還是盡量用自己真實(shí)的數(shù)據(jù)去做測(cè)試,以選擇最適合自己數(shù)據(jù)的分配器。
以上就是小編為大家?guī)淼臏\談redis采用不同內(nèi)存分配器tcmalloc和jemalloc全部?jī)?nèi)容了,希望大家多多支持腳本之家~
您可能感興趣的文章:- 查看Redis內(nèi)存信息的命令
- 一次關(guān)于Redis內(nèi)存詭異增長(zhǎng)的排查過程實(shí)戰(zhàn)記錄
- 淺談redis內(nèi)存數(shù)據(jù)的持久化方式
- 降低PHP Redis內(nèi)存占用
- 將MongoDB作為Redis式的內(nèi)存數(shù)據(jù)庫(kù)的使用方法
- Redis教程(十四):內(nèi)存優(yōu)化介紹
- Redis教程(十一):虛擬內(nèi)存介紹
- redis數(shù)據(jù)庫(kù)查找key在內(nèi)存中的位置的方法
- Redis fork進(jìn)程分配不到內(nèi)存解決方案