I have a long running operation in PHP and it always crashed with an out of memory.
So i started logging mem usage with:
$result = memory_get_usage() / 1024;
echo $result;
By commenting parts of the code I found the "guilty" one, responsible for eating up all my ram.
This is the code:
static private function from_camel_case($str)
{
$str[0] = strtolower($str[0]);
$func = create_function('$c', 'return "_" . strtolower($c[1]);');
$result = preg_replace_callback('/([A-Z])/', $func, $str);
return $result;
}
It basically converts text in camelcase to lowercase with underscores.
Why is this leaking?
I am running PHP 5.3.5 in MAMP on my Mac OS X Lion
preg_replaceto prefix upper cased letter with_and then applystrtolowerto the whole string. It will prevent of using callbacks and creating functions