PHP Object Creation and Memory Usage

Posted by JohnO on Stack Overflow See other posts from Stack Overflow or by JohnO
Published on 2009-01-22T14:46:50Z Indexed on 2010/03/29 14:53 UTC
Read the original article Hit count: 212

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about php

Related posts about memory