主頁 > 知識庫 > 淺談redis采用不同內(nèi)存分配器tcmalloc和jemalloc

淺談redis采用不同內(nèi)存分配器tcmalloc和jemalloc

熱門標簽:AI電銷 Linux服務器 網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) 地方門戶網(wǎng)站 呼叫中心市場需求 百度競價排名 服務外包

我們知道Redis并沒有自己實現(xiàn)內(nèi)存池,沒有在標準的系統(tǒng)內(nèi)存分配器上再加上自己的東西。所以系統(tǒng)內(nèi)存分配器的性能及碎片率會對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在編譯時,會先判斷是否使用tcmalloc,如果是,會用tcmalloc對應的函數(shù)替換掉標準的libc中的函數(shù)實現(xiàn)。其次會判斷jemalloc是否使得,最后如果都沒有使用才會用標準的libc中的內(nèi)存管理函數(shù)。

而在最新的2.4.4版本中,jemalloc已經(jīng)作為源碼包的一部分包含在源碼包中,所以可以直接被使用。而如果你要使用tcmalloc的話,是需要自己安裝的。

下面簡單說一下如何安裝tcmalloc包,tcmalloc是google-proftools中的一部分,所以我們實際上需要安裝google-proftools。如果你是在64位機器上進行安裝,需要先安裝其依賴的libunwind庫。

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

然后再進行google-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 #如果沒有這個文件,自己建一個 sudo /sbin/ldconfig

然后再進行Redis的安裝,在make時指定相應的參數(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

再啟動Redis后通過info命令就能看到使用的內(nèi)存分配器了。

下面回到本文的主題,對于tcmalloc,jemalloc和libc對應的三個內(nèi)存分配器。其性能和碎片率如何呢?

下面是一個簡單測試結果,使用Redis自帶的redis-benchmark寫入等量數(shù)據(jù)進行測試,數(shù)據(jù)摘自采用不同分配器時Redis info信息。

我們可以看到,采用tcmalloc時碎片率是最低的,為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

上面的測試數(shù)據(jù)都是小數(shù)據(jù),也就是說單條數(shù)據(jù)并不大,下面我們嘗試設置benchmark的-d參數(shù),將value值調(diào)整為1k大小,測試結果發(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ù)據(jù)去做測試,以選擇最適合自己數(shù)據(jù)的分配器。

以上就是小編為大家?guī)淼臏\談redis采用不同內(nèi)存分配器tcmalloc和jemalloc全部內(nèi)容了,希望大家多多支持腳本之家~

您可能感興趣的文章:
  • 查看Redis內(nèi)存信息的命令
  • 一次關于Redis內(nèi)存詭異增長的排查過程實戰(zhàn)記錄
  • 淺談redis內(nèi)存數(shù)據(jù)的持久化方式
  • 降低PHP Redis內(nèi)存占用
  • 將MongoDB作為Redis式的內(nèi)存數(shù)據(jù)庫的使用方法
  • Redis教程(十四):內(nèi)存優(yōu)化介紹
  • Redis教程(十一):虛擬內(nèi)存介紹
  • redis數(shù)據(jù)庫查找key在內(nèi)存中的位置的方法
  • Redis fork進程分配不到內(nèi)存解決方案

標簽:黃山 崇左 銅川 蘭州 湖南 衡水 仙桃 湘潭

巨人網(wǎng)絡通訊聲明:本文標題《淺談redis采用不同內(nèi)存分配器tcmalloc和jemalloc》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266