memory usage patch

Timo Ewalds timo at
Fri Aug 24 16:52:53 UTC 2007

Here is a simple patch against 1.2.2 that should increase the memory 
usage efficiency in certain cases. We've been running it stable for a 
few days without problems, and it works as expected. It just checks to 
see if the last item in the LRU is expired before allocating space for 
the new item. In the case that it is expired, remove it before 
allocating. That has the effect of replacing the oldest item in that 
size, if possible. It won't help in all cases, but if slabs have 
different write and expiry profiles, it may help keep them in check. In 
the extreme case, it may even keep the total memory usage well below the 
maximum, when it would currently fill with expired data. It would be a 
big help if certain slab sizes see much higher write load than others, 
but those writes have a short expiry time.

-------------- next part --------------
--- items.c.orig        2007-08-16 14:46:18.000000000 -0600
+++ items.c     2007-08-16 14:46:51.000000000 -0600
@@ -82,6 +82,11 @@
     if (id == 0)
         return 0;
+    /* Try to keep memory usage to a minimum by removing/replacing the last item
+     * in the queue if it has already expired, instead of allocating more space.
+     */
+    if (tails[id] != 0 && tails[id]->refcount == 0 && tails[id]->exptime != 0 && tails[id]->exptime < current_time)
+        do_item_unlink(tails[id]);
     it = slabs_alloc(ntotal);
     if (it == 0) {
         int tries = 50;

More information about the memcached mailing list