Memcached Memory Management algorithm

Source: Internet
Author: User
Tags int size memcached

Simple writing, after reading this part of the code memcached and I feel that the ccache is still very similar.

1) Distribution

All memory in memcached is saved by the type slabclass_t structure

typedef struct {
unsigned int size;      /* sizes of items */
unsigned int perslab;   /* how many items per slab */

void **slots;           /* list of item ptrs */
unsigned int sl_total;  /* size of previous array */
unsigned int sl_curr;   /* first free slot */

void *end_page_ptr;         /* pointer to next free item at end of page, or 0 */
unsigned int end_page_free; /* number of items remaining at end of last alloced page */

unsigned int slabs;     /* how many slabs were allocated for this class */

void **slab_list;       /* array of slab pointers */
unsigned int list_size; /* size of prev array */

unsigned int killing;  /* index+1 of dying slab, or zero if none */
} slabclass_t;

There is a global slabclass_t array, and the size field in slabclass_t holds the amount of data that each slab can hold. In this slabclass_t array, the Size field is incremented and the incremented factor is determined by the Slabs_ The second parameter in the INIT function is specified by the factor parameter. For example, if factor is 2, then if the size of the first slabclass_t is unsigned int size = sizeof (item) + SETTINGS.CHUNK_ Size (also the statement in the Slabs_init function), the size of the next slabclass_t is Size*factor (this ignores the alignment factor).

So, assuming the first slab can save 8byte of data, Factor is 2, then the next slab size is 16byte,32byte ...

Each time you need to allocate memory, you need to find a size larger than the minimum size of the slab, for example, or the previous slab model, if you need to allocate 30byte of space now, find the minimum slab size greater than 30byte is 32byte, So, look up the item from this slab and assign it to it.

But here is a problem, that is, the waste of surplus resources, the previous 30byte is just a waste of 2byte, but if you want to allocate 17byte, then wasted 15byte, wasted nearly 50%! So there's the reason why you need to specify factor. Users can specify different growth factor as needed to reduce the waste of resources.

2) elimination

Eliminated is the LRU algorithm, all the most recently used item is saved in the static item *tails[largest_id];(ITEM.C), the allocated memory will be stored in the form of a linked list in this array, if the corresponding slab has not allocated enough memory, To this list of inquiries, the elimination is based on the item structure of the Exptime field.

Simple analysis to this, need a more detailed explanation to see the code, in memcached with this part of the code in Slab.h (. c)/item.h (. c), two key structures are item and slabclass_t.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.