Memcached’s memory allocation is in page units. By default, one page is 1M, which can be specified at startup through the -I parameter. If you need to apply for memory, memcached will divide a new page and allocate it to the required slab area. Once a page is allocated, it will not be recycled or reallocated before restarting
Memcached does not put all sizes of data together,but pre-registers the data The space is divided into a series of slabs, and each slab is only responsible for data storage within a certain range. As shown in the figure below, each slab only stores data that is larger than the size of the previous slab and smaller than or equal to its own maximum size. For example: slab 3 only stores data between 137 and 224 bytes. If a data size is 230byte it will be allocated to slab 4. As can be seen from the figure below, the space responsible for each slab is actually not equal. memcached By default, the maximum value of the next slab is 1.25 times the previous one. This can be modified by
Change the -f parameter to modify the growth ratio.
Chunk is a series of fixed memory spaces. This size is the maximum storage size of the slab that manages it. For example: all chunks of slab 1 are 104byte, and all chunks of slab 4 are 280byte. Chunk is where memcached actually stores cached data. Because the chunk size is fixed to the maximum value that the slab can store, all data allocated to the current slab can be stored in the chunk. If the time data size is smaller than the chunk size, the free space will be idle. This is designed to prevent memory fragmentation. For example, in the figure below, chunk
The size is 224byte, and the stored data is only 200byte, and the remaining 24byte will be idle.
but this will not be occupied as soon as it is started, and is gradually allocated to each slab as needed.
If a new cache data is to be stored, memcached first selects a suitable slab, and then checks whether there are any free chunks in the slab. If so, it will be stored directly; if not, it will be applied for.Slab uses page as the unit when applying for memory, so when the first data is put in, no matter what the size is, a 1M page will be allocated to the slab. After applying for a page, slab will divide the memory of this page according to the size of the chunk, so that it becomes an array of chunks, and then select one from the chunk array to store data. As shown below, slab 1 and slab
2 allocate a page and divide it into chunk arrays according to their respective sizes.
Based on the above introduction, the memory allocation strategy of memcached is: allocate pages according to slab requirements, and each slab uses chunk storage as needed.
There are several features to note here: