日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

Memcached内存存储

發(fā)布時(shí)間:2025/4/14 64 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Memcached内存存储 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

早就聽(tīng)說(shuō)過(guò)Memcached獨(dú)特的內(nèi)存管理方式,寫(xiě)著篇文章的目的就是了解Memcached的內(nèi)存管理,學(xué)習(xí)其源代碼.

1.什么是Slab Allocator

memcached默認(rèn)情況下采用了名為Slab Allocator的機(jī)制分配、管理內(nèi)存,Slab Allocator的基本原理是按照預(yù)先規(guī)定的大小,將分配的內(nèi)存分割成特定長(zhǎng)度的塊,以期望完全解決內(nèi)存碎片問(wèn)題。而且,slab allocator還有重復(fù)使用已分配的內(nèi)存的目的。 也就是說(shuō),分配到的內(nèi)存不會(huì)釋放,而是重復(fù)利用。

2.Slab Allocation的主要術(shù)語(yǔ)

Page 分配給Slab的內(nèi)存空間,默認(rèn)是1MB,分配給Slab之后根據(jù)slab的大小切分成chunk Chunk 用于緩存記錄的內(nèi)存空間 Slab Class 特定大小的chunk的組

3.Slab初始化

在Memcached啟動(dòng)時(shí)候會(huì)調(diào)用slab的初始化代碼(詳見(jiàn)memcached.c中main函數(shù)調(diào)用slabs_init函數(shù)).

slabs_init函數(shù)聲明:

1 2 3 4 5 6 7 /** Init the subsystem. 1st argument is the limit on no. of bytes to allocate, 0 if no limit. 2nd argument is the growth factor; each slab will use a chunk size equal to the previous slab's chunk size times this factor. 3rd argument specifies if the slab allocator should allocate all memory up front (if true), or allocate memory in chunks as it is needed (if false) */ void slabs_init(const size_t limit, const double factor, const bool prealloc);

其中l(wèi)imit表示memcached最大使用內(nèi)存;factor表示slab中chunk size的增長(zhǎng)因子,slab中chunk size的大小等于前一個(gè)slab的chunk size乘以factor;

memcached.c中main函數(shù)調(diào)用slabs_init函數(shù):

1 slabs_init(settings.maxbytes, settings.factor, preallocate);

其中settings.maxbytes默認(rèn)值為64M,啟動(dòng)memcached使用選項(xiàng)-m設(shè)置;settings.factor默認(rèn)為1.25,啟動(dòng)memcached時(shí)候使用-f設(shè)置;preallocate指的是啟動(dòng)memcached的時(shí)候默認(rèn)為每種類(lèi)型slab預(yù)先分配一個(gè)page的內(nèi)存,默認(rèn)是false;

1 2 3 4 5 settings.maxbytes = 64 * 1024 * 1024; /* default is 64MB */ ... settings.factor = 1.25; ... preallocate = false

slabs_init函數(shù)實(shí)現(xiàn):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 /** * Determines the chunk sizes and initializes the slab class descriptors * accordingly. */ void slabs_init(const size_t limit, const double factor, const bool prealloc) { int i = POWER_SMALLEST - 1; //真實(shí)占用大小=對(duì)象大小+48 unsigned int size = sizeof(item) + settings.chunk_size; mem_limit = limit; //開(kāi)啟預(yù)分配,則首先將limit大小(默認(rèn)64M)的內(nèi)存全部申請(qǐng) if (prealloc) { /* Allocate everything in a big chunk with malloc */ mem_base = malloc(mem_limit); if (mem_base != NULL) { mem_current = mem_base; mem_avail = mem_limit; } else { fprintf(stderr, "Warning: Failed to allocate requested memory in" " one large chunk.\nWill allocate in smaller chunks\n"); } } //清空所有的slab memset(slabclass, 0, sizeof(slabclass)); while (++i < POWER_LARGEST && size <= settings.item_size_max / factor) { /* Make sure items are always n-byte aligned */ if (size % CHUNK_ALIGN_BYTES) size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES); slabclass[i].size = size; slabclass[i].perslab = settings.item_size_max / slabclass[i].size; size *= factor; if (settings.verbose > 1) { fprintf(stderr, "slab class %3d: chunk size %9u perslab %7u\n", i, slabclass[i].size, slabclass[i].perslab); } } //最大chunksize的一個(gè)slab,chunksize為settings.item_size_max(默認(rèn)1M) power_largest = i; slabclass[

轉(zhuǎn)載于:https://www.cnblogs.com/codershuai/p/6150002.html

總結(jié)

以上是生活随笔為你收集整理的Memcached内存存储的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。