Memory allocation mechanism Slab Allocation
This article refers to the blog: https://my.oschina.net/bieber/blog/505458
Memcached's memory allocations are in slabs, which actually divides different slabs according to the initial chunk size, growth factor, and the size of the stored data Class,slab The class contains several equal-sized trunks and a fixed 48byte item information. Trunks are stored by page, and each page becomes a pages (default 1M).
1.slabs, Slab class, Page three relations:
Slabs = Slab Class1 + Slab Class2 + ... + slab classn
Sbal Class = trunksize * Trunkcount * PageCount
Trunkcount = Pagesize/trunksize
Trunksize = Actual data size + 48byte (items data structure)
Example: Assuming that each Slab class page is 1, then slab Class1 = 88byte * Trunkcount * 1
When starting memcached with the-VV parameter key output slabs information, we can see that the slabs data is based on growth factor increment, but the data will be slightly error ~
2. The actual data storage will choose the appropriate space, for example, I want to store a 52byte of data, the actual need for 100byte space
52+ = 100byte, the data will be stored in 112bytes corresponding to the SLABLS, occupy a trunk
3. Mechanism memory waste problem:
Advantage: The memory allocation mechanism was previously malloc~free, and there was a memory fragmentation issue. This mechanism solves the problem of memory fragmentation
Disadvantage: If the growth factor is not properly set, it may cause a waste of space. Because the trunk size is fixed, the data can only be adapted to the size of the trunk (<=trunk)
Memory usage mechanism LRU
When the data in the memcached expires, the associated memory is not released directly because there is no response to the listener to handle the incident. Flush_all also does not release memory, except that the failed data is transparent to the user and the user cannot retrieve the data. Memcached allocated memory is not recycled, but can be reused. Memcached will prefer to use memory that has expired. When memory is low, long-term unused memory is allocated to new records through the LRU mechanism.
Note: The start parameter with-M does not support LRU operation
Common monitoring
Telnet command line, direct operation stats, stats slabs and other commands for analysis
memcached.php = "PHP system using Apache service, graphical interface
Daemontools = "Don't know what it is, write it down first."
Nagios = "Checktcp =" don't know what it is, write it down first.
Optimization ideas
1. Reasonable growth factor and control the reasonable consumption of memory
2. cache update mechanism = Update the cache when it is about to expire
Memcached Learning--memcached memory allocation mechanism slab Allocation, memory usage mechanism LRU, common monitoring record (iv)