Thursday, June 18, 2009

Memory leak in xcache extension of PHP

I recently encountered memory leak while using the xcache extension (version 1.2) with PHP 5.2.6.

The following code confirms the memory leak:

$data = "data";
xcache_set("key",$data);
while(1) {
$a = xcache_get("key");
echo "Memory Usage :".memory_get_usage()."\n";
}

This seems like a known issue and a worksround is suggested by oli at http://xcache.lighttpd.net/ticket/95 . Simply type-cast the data to a string while using xcache_set. The following works without any memory leak:

$data = "data";
xcache_set("key",(string)$data);
while(1) {
$a = xcache_get("key");
echo "Memory Usage :".memory_get_usage()."\n";
}

Don't know the root cause of this issue but the workaround helped.

1 comment:

Tixik said...

Mem leaks are often caused by too high var memory value. If your box completely get down in few hours after apache is restarted, try to lower down these values and you will see.