Memcache memory allocation mechanism

WBOY
Release: 2016-08-08 09:20:14
Original
874 people have browsed it
Page (page) is the smallest unit of memory allocation.

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
Memcache memory allocation mechanism

Slabs (layers) divide the data space

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.
Memcache memory allocation mechanism

Chunk (block) is the unit for storing cached data.

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.
Memcache memory allocation mechanism

Slab’s memory allocation. Memcached specifies the maximum memory usage through -m when starting,

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.
Memcache memory allocation mechanism

Memcached memory allocation strategy.

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:

    The pages allocated by Memcached will not be recycled or reallocated
  • The memory applied by Memcached will not be released
  • The idle chunks of the slab will not be lent to any other slab.
This article is reproduced from: http://blog.csdn.net/u013927110/article/details/47024499 The above has introduced the Memcache memory allocation mechanism, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template