14

I've been trying to clear my memcache as I'm noticing the storage taking up almost 30% of server memory when using ps -aux.

So I ran the following php code.

$memcache = new Memcache;
    $memcache->connect("localhost",11211);
    $memcache->flush();
    print_r($memcache->getStats());

This results in the output of

(
    [pid] => 4936
    [uptime] => 27318915
    [time] => 1255318611
    [version] => 1.2.2
    [pointer_size] => 64
    [rusage_user] => 9.659531
    [rusage_system] => 49.770433
    [curr_items] => 57864
    [total_items] => 128246
    [bytes] => 1931734247
    [curr_connections] => 1
    [total_connections] => 128488
    [connection_structures] => 17
    [cmd_get] => 170288
    [cmd_set] => 128246
    [get_hits] => 45464
    [get_misses] => 124824
    [evictions] => 1009
    [bytes_read] => 5607431213
    [bytes_written] => 1806543589
    [limit_maxbytes] => 2147483648
    [threads] => 1
)

This should be fairly basic, but clearly, I'm missing something.

jensgram
  • 31,109
  • 6
  • 81
  • 98
pedalpete
  • 21,076
  • 45
  • 128
  • 239

5 Answers5

17

You really need to change the memcached settings so that it doesn't use up as much memory. When you start memcached, you can pass it the amount of memory it should use, in megabytes, using the -m flag. See its documentation for information.

flush just invalidates all of the items in the cache, it doesn't command memcached to deallocate or unreserve the memory it is using. I doubt that you can command memcached to deallocate the memory it is using.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Thanks James. That does clear it up. I purposefully gave memcache lots of memory, but apparently much more than was needed. I'll restart the memcache with lower settings. – pedalpete Oct 12 '09 at 04:36
  • This is still a valid question though.. I am on a shared environment that uses a memcache server for Magento and it seems that the above does not work! I don't have privileges to restart the server but the dirty cache has to go before my site will work properly so for the time being I am stuck with a file cache. – ColinM Oct 27 '09 at 11:08
13

Colin, The Flush All command causes the cache to set all the expiration times to current. The next request for an existing key will return nothing and the record will be removed from the cache. Memcached does not have a separate process to clean expired items and uses a "Lazy" method which makes the process very lightweight and efficient. Because of this if you need to actually remove the cache and start from scratch, the only real way to accomplish this is to restart Memcached. A long workaround would be to dump all your keys, send the Flush All command, then loop through each key running a get against it, causing the record to be removed. Not 100% sure if this method would work but in theory sounds plausible.

hcs
  • 321
  • 3
  • 14
Gary Steven
  • 156
  • 2
2

You need to wait atleast 1 second after clearing memcache. otherwise items added less than one second will invalidate itself.

Ex:

$memcache->flush();

$time = time()+1; //one second future
while(time() < $time) {
//sleep
}
$memcache->set('key', 'value'); // repopulate the cache 

Look at this post, memcache flush issue

vkGunasekaran
  • 6,668
  • 7
  • 50
  • 59
  • 1
    this helped me, but instead of using while you could just use `sleep(1)` this is more cpu friendly and of course more readable. – Roey Jan 11 '15 at 08:59
0

Actually, the easiest way to deallocate all values is restart memcached instance.

mulya
  • 1,273
  • 13
  • 26
  • 1
    This is NOT a good idea. If you have an extremely busy website, restarting memcache can actually cause issues. For example, I run a Wordpress site with 1,000 visitors concurrently. If I restart the memcache cluster the MySQL backend can't serve data fast enough and WordPress thinks its a new install. Always FLUSH if possible. Restart only if no other choice. – frustratedtech Nov 29 '15 at 16:10
  • Agree, you always have to know what you doing. – mulya Nov 29 '15 at 17:15
-5

Try this

Mage::app()->getCacheInstance()->getFrontend()->getBackend()->clean(Zend_Cache::CLEANING_MODE_ALL);

Rom
  • 1