first, memcache memory allocation mechanism
There are a lot of explanations about this mechanism on the Internet, and my personal summary is as follows.
the page is the smallest unit of memory allocation.
The memory allocation for memcached is in page, by default a page is 1M, which can be specified at startup by the-I parameter. If you need to request memory, memcached divides a new page and assigns it to the desired slab area. Page will not be recycled or redistributed once it is allocated (page ressign has been removed from version 1.2.8)
slabs divides the data space.
Instead of memcached all the data of all sizes together, the data space is pre-divided into a series of slabs, each slab only responsible for a range of data stores. For example, each slab only stores data that is larger than its previous slab size and less than or equal to its maximum size. For example: Slab 3 only stores data with sizes ranging from 137 to 224 bytes. If a data size of 230byte will be assigned to Slab 4. As can be seen, each slab responsible for the space is actually unequal, memcached by default, the next slab maximum value is 1.25 times times the previous one, this can be modified by modifying the-f parameter to modify the growth ratio.
Chunk is the unit in which the cached data is stored.
Chunk is a fixed set of memory space, which is the maximum size of the slab that manages it. For example: All chunk of slab 1 are 104byte, and all chunk of slab 4 are 280byte. Chunk is where the memcached actually stores the cached data, because the chunk is fixed to the maximum value slab can hold, so all data allocated to the current slab can be saved by chunk. If the data size of the time is less than the size of the chunk, the free space will be idle, this is designed to prevent memory fragmentation. For example, the chunk size is 224byte, and the stored data is only 200byte, and the remaining 24byte will be idle.
memory allocation for slab.
memcached specifies the maximum memory to use by-m at startup, but this does not take up as soon as it is started, and is gradually assigned to each slab.
If a new cached data is to be stored, memcached first selects a suitable slab and then checks to see if the slab has free chunk, and if so, it is stored directly; Slab requests Memory in page, so when the first data is placed, regardless of size, a page of 1M size is assigned to the slab. After applying to the page, slab will slice the page's memory by the size of chunk, so it becomes an array of chunk, selecting one from the chunk array to store the data. For example, Slab 1 and Slab 2 are assigned a page and are divided into chunk arrays by their respective sizes.
memcached memory allocation policy.
To synthesize the above introduction, Memcached's memory allocation strategy is to allocate page according to slab requirements, each slab use chunk storage on demand.
Here are a few features to note,
Memcached assigned page will not be reclaimed or redistributed memcached requested memory will not be released slab idle chunk will not be lent to any other slab use
Knowing this, you can understand why the total memory is not fully occupied, memcached but there is a loss of cached data problems.
Memcache Memory allocation Policy