Memcached Memory Allocation
Join the DZone community and get the full member experience.
Join For Freemany developers use memcached for their projects, but only a few of them know exactly how this caching solution works. let’s dive into the world of memcached to learn more about its memory allocation and how to prevent fragmented memory for your java and php applications.
memcached uses a slab memory allocation, instead of allocating the memory on an item by item basis. this allows for improvement to memory usage and prevents memory fragmentation, when information expires from the cache. in this case, memory is allocated in blocks of 1mb in size. each slab is separated into a number of identical blocks. when you try to store a specified value into the cache, memcached checks its size and defines which slab contains a suitably sized allocation. if such a slab already exists, the value is written to the block within the slab. if there are no existing blocks that are suitable, then a new slab is created and separated into blocks of an appropriate size. if you update an existing item with larger data than the existing block allocation for that key, then the key is reallocated into a suitable slab.
as a result, you get multiple pages allocated within the memcached memory. each page is 1mb in size and is divided into a different number of chunks. every instance has multiple pages distributed and a new page is always created when a new value needs to be created, requiring a chunk of a particular size. a slab may contain multiple pages, and each page consists of an equal number of chunks.
such a memory allocation method ensures that memory does not get fragmented. but remember that having a relatively small number of items within each chunk size, may waste a lot of memory with just a few chunks in each allocated page. the efficiency of using such an approach fully depends on the distribution of the objects that you want to store.
the jelastic platform allows you to change the coefficient of slab growth while the application is running. simply click the config button for memcached and edit your memcached file, which is located in the conf directory.
let’s perform a small test!
for example:
options="-vv 2>> /var/log/memcached/memcached.log -f 2 -n 32"
in this case -f 2 means that you will see 14 slabs and their chunk size doubles, -n is used to specify the minimum space allocated for key, value and flags.
here are the result of our small test:
-
chunk details:
# item_size max_age pages count full? evicted evict_time oom 3 320b 550s 1 113 yes 0 0 0 4 640b 681s 1 277 yes 0 0 0
-
memory usage:
total used free shared buffers cached mem: 128 84 43 0 0 70 -/+ buffers/cache: 14 113 swap: 0 0
now you can compare it with our memcached default configuration:
options="-vv 2>> /var/log/memcached/memcached.log"
the results are:
-
chunk details:
# item_size max_age pages count full? evicted evict_time oom 5 240b 765s 1 27 yes 0 0 0 6 304b 634s 1 93 yes 0 0 0 7 384b 634s 1 106 yes 0 0 0 8 480b 703s 1 133 yes 0 0 0 9 600b 634s 1 57 yes 0 0 0
-
memory usage:
total used free shared buffers cached mem: 128 87 40 0 0 70 -/+ buffers/cache: 17 110 swap: 0 0 0
you can also use -l to increase the memory page size in order to reduce the number of tlb misses and to improve the performance.
thanks to this easy and straightforward optimization we can improve the usage of the allocated memory.
it can be very useful to take a deeper look into different systems. as you can see, they are much more complex than we can imagine. they consist of a number of methods, which can help to solve a variety of problems. knowledge and understanding of the basics of each technology will help you in your development.
Published at DZone with permission of Marina Sprava, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments