Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memcache won't flush or clear memory

Tags:

php

memcached

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.

like image 625
pedalpete Avatar asked Oct 12 '09 03:10

pedalpete


People also ask

How do I delete Memcache data?

Memcached flush_all command is used to delete all data (key-value pairs) from the Memcached server. It accepts an optional parameter called time that sets a time after which the Memcached data is to be cleared.

What happens when memcache is full?

First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.

How can I clear Memcached data in PHP?

Memcached flush_all command is used to clear all the data (i.e. key value pairs) from the Memcached server. Means, this command invalidates all existing cache items. It accepts an optional parameter, which means to invalidate all items after N seconds have passed.

How does memcache store data?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.


2 Answers

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.

like image 51
James McNellis Avatar answered Sep 28 '22 13:09

James McNellis


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.

like image 36
Gary Steven Avatar answered Sep 28 '22 13:09

Gary Steven