我們知道Redis并沒(méi)有自己實(shí)現(xiàn)內(nèi)存池,,沒(méi)有在標(biāo)準(zhǔn)的系統(tǒng)內(nèi)存分配器上再加上自己的東西,。所以系統(tǒng)內(nèi)存分配器的性能及碎片率會(huì)對(duì)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在編譯時(shí),會(huì)先判斷是否使用tcmalloc,,如果是,,會(huì)用tcmalloc對(duì)應(yīng)的函數(shù)替換掉標(biāo)準(zhǔn)的libc中的函數(shù)實(shí)現(xiàn)。其次會(huì)判斷jemalloc是否使得,,最后如果都沒(méi)有使用才會(huì)用標(biāo)準(zhǔn)的libc中的內(nèi)存管理函數(shù),。 而在最新的2.4.4版本中,jemalloc已經(jīng)作為源碼包的一部分包含在源碼包中,,所以可以直接被使用,。而如果你要使用tcmalloc的話,是需要自己安裝的,。 下面簡(jiǎn)單說(shuō)一下如何安裝tcmalloc包,,tcmalloc是google-proftools中的一部分,所以我們實(shí)際上需要安裝google-proftools,。如果你是在64位機(jī)器上進(jìn)行安裝,,需要先安裝其依賴(lài)的libunwind庫(kù)。 wget http://download.savannah./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./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 #如果沒(méi)有這個(gè)文件,,自己建一個(gè) sudo /sbin/ldconfig 然后再進(jìn)行Redis的安裝,,在make時(shí)指定相應(yīng)的參數(shù)以啟用tcmalloc $ curl -O http://redis./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后通過(guò)info命令就能看到使用的內(nèi)存分配器了,。 下面回到本文的主題,對(duì)于tcmalloc,,jemalloc和libc對(duì)應(yīng)的三個(gè)內(nèi)存分配器,。其性能和碎片率如何呢?下面是一個(gè)簡(jiǎn)單測(cè)試結(jié)果,,使用Redis自帶的redis-benchmark寫(xiě)入等量數(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.01 mem_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.02 mem_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.31 mem_allocator:libc 上面的測(cè)試數(shù)據(jù)都是小數(shù)據(jù),也就是說(shuō)單條數(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.02 mem_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.01 mem_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.04 mem_allocator:libc 可以看出,,在分配大塊內(nèi)存和小塊內(nèi)存上,,幾種分配器的碎片率差距還是比較大的,大家在使用Redis的時(shí)候,,還是盡量用自己真實(shí)的數(shù)據(jù)去做測(cè)試,以選擇最適合自己數(shù)據(jù)的分配器,。 |
|
來(lái)自: lchjczw > 《內(nèi)存深入研究》