Memcached Memory Allocation

Many 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 the suitable size 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, -nis used to specify the minimum space allocated for key, value and flags.

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.

CONTACT

Registration failed

The email address is not valid. Double check the address and try again.

Registration failed

We suspect that this is due to network issues - so please try again in a few minutes. If your second attempt fails, please email us at info@jelastic.com and we will assist you with the creation of your account.