tags:

views:

1887

answers:

3

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.

+4  A: 

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
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
You're welcome.
James McNellis
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
Ask it as a question.
James McNellis
A: 

Colin, The Flush All command causes the cache to set all the experation times to current. The next request for an existing key will return nothing and the record will be removed from the cache. Since Memcached does not have a seperate process to clean expired items and uses a "Lazy" method which makes the process very light weight and efficient, however 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 work around 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. I dont know for 100% if this method would work but in theory sounds plausible.

Gary Steven
A: 

Try this

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

Rom