PHP Object Creation and Memory Usage
- by JohnO
A basic dummy class:
class foo
{
var $bar = 0;
function foo() {}
function boo() {}
}
echo memory_get_usage();
echo "\n";
$foo = new foo();
echo memory_get_usage();
echo "\n";
unset($foo);
echo memory_get_usage();
echo "\n";
$foo = null;
echo memory_get_usage();
echo "\n";
Outputs:
$ php test.php
353672
353792
353792
353792
Now, I know that PHP docs say that memory won't be freed until it is needed (hitting the ceiling). However, I wrote this up as a small test, because I've got a much longer task, using a much bigger object, with many instances of that object. And the memory just climbs, eventually running out and stopping execution. Even though these large objects do take up memory, since I destroy them after I'm done with each one (serially), it should not run out of memory (unless a single object exhausts the entire space for memory, which is not the case).
Thoughts?