memcached memory usage

Denis Antipov antipovd at
Wed Dec 7 07:23:02 UTC 2005

Hello, folks

I've been continuing to test memcached and trying to go about my problem
with overwritten/lost data in memcached without the use of a database or
some other solid data source. Yes I'm stubborn. Anyway. I've written a
script that shows, IMHO, that memcached doesn't utilize memory efficiently,
either due to it's queue and class slab allocation algorithm, or some other
unknown to me reason. See the PHP script below. I've used Apache Bench with
50000 requests on a memcached with 1Gb maxbytes. The script outputs a CSV
line for each data "set" in memcached. It records time, current items, total
items, the key, and the data size that was stored for that key (the amount
of data is random for each key). For my run, I noticed that at about 15K
items no more bytes were "used", meaning that no more memory was allocated
and no more items can be stored, the overwriting has begun. The problem is
that bytes kept staying at 560Mb which means it's using 56% of the allowed
memory. I've read about memory space inefficiency with the classes and such,
but 50%!!! Isn't it a bit low?!!! Can we address this issue somehow, was
there anything done besides the class reasignment script in scripts, which
I'm not sure how would solve this problem anyway?



$sess = session_id();

$data_file = "data.csv";
$fail_file = "conn_fail.log";
$flag = 'a';

$memcache = new Memcache;
if(!$memcache->connect('', 11213)){
   $flag = 'a';
 } else {
   $flag = 'w';
   $ffh = fopen($fail_file, $flag) or die ("unable to open ".$fail_file);
   while(1) {
     if (flock($ffh, LOCK_EX)) {
     fwrite($ffh, $sess."\n") or die ("failed to write to ".$fail_file);
         flock($ffh, LOCK_UN);

} else {
  for($i=rand(1,13);$i>0;$i--){ // 13 is no not go over the 1MB limit

  $size = strlen($sess)*2; // 2 bytes per character

  $stats = $memcache->getStats();

  if($memcache->set(session_id(), $sess, false, 0)){
      $flag = 'a';
    } else {
      $flag = 'w';

    $dh = fopen($data_file,$flag) or die ("unable to open ".$data_file);
    while(1) {
      if (flock($dh, LOCK_EX)) {

      or die ("failed to write to ".$data_file);
         flock($fdh, LOCK_UN);



"To a person with a hammer, all problems look like nails"
someone on Usenet
-------------- next part --------------
An HTML attachment was scrubbed...

More information about the memcached mailing list