views:

709

answers:

2

There is a way to get the total memory PHP is using (memory_get_usage()) but how does one get the size in memory of an individual object?

I'm obviously not talking about count() as I want the number of bytes in a potentially complex data structure.

Is this even possible?

Thanks.

+3  A: 

I don't think this is quite possible ; I've never seen anything that would allow you to get the size of an object in memory.

A solution to get a pretty rough idea might be to kind of serialize your data, and use strlen on that... But that will really be something of an estimation... I wouldn't quite rely on anything like that, actually...


Even debug_zval_dump doesn't do that : it ouput the data in a variable and the refcount, but not the memory used :

$obj = new stdClass();
$obj->a = 152;
$obj->b = 'test';

echo '<pre>';
debug_zval_dump($obj);
echo '</pre>';

will only get you :

object(stdClass)#1 (2) refcount(2){
  ["a"]=>
  long(152) refcount(1)
  ["b"]=>
  string(4) "test" refcount(1)
}
Pascal MARTIN
Very interesting. My next question was actually about getting refcounts to figure out why there is a memory leak. Thanks!
Artem Russakovskii
+1  A: 

You can call memory_get_usage() before and after allocating your class as illustrated in this example from IBM. You could even create a wrapper to do this, possibly storing the result on a member variable of the complex class itself.

EDIT:

To clarify the part about storing the allocated memory size, you can do something like this:

class MyBigClass
{
    var $allocatedSize;
    var $allMyOtherStuff;
}

function AllocateMyBigClass()
{
    $before = memory_get_usage();
    $ret = new MyBigClass;
    $after = memory_get_usage();
    $ret->allocatedSize = ($after - $before);

    return $ret;
}

At any point in the future, you could check allocatedSize to see how big that object was at time of allocation. If you add to it after allocating it, though, allocatedSize would no longer be accurate.

Eric J.
What if PHP decides to free the memory that was kept allocated, just at the "wrong time" ? I don't really know when PHP frees memory (kind of "when needed", I suppose), but I'm guessing this freeing could bring some problems ? Especially with the garbage collector introduced by PHP 5.3 for cyclic-references ?
Pascal MARTIN
Well yeah, but in addition to what Pascal mentioned, I want to be able to find this out at different times, not just at allocation time. I want to find this out many lines down the road.
Artem Russakovskii
@Pascal: PHP will not free memory that's still referenced by an object actively being used. Cyclic references means A references B and B references A, but nothing else references either A or B. So memory will not be freed as long as the program can still reference it in any way.
Eric J.
@Artem: I mentioned in my post that you could keep the allocated size as part of your data structure. I'll edit the post now to make that clearer.
Eric J.