Home » Php » oop – Getting size in memory of an object in PHP?

oop – Getting size in memory of an object in PHP?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

Would it not make sense to try serializing the object and reading the string length? Obviously it will be several bytes off because serialized string would have s:’string’ therefore s:” being extra bytes… unless serialize could be the same way that PHP stores objects???

so for example

$size = strlen(serialize($object));

Just a thought?

Another messy but possibly accurate thought:

Assuming a class instance variable that has been manipulated a few times since instantiation:

$DB; // database access class for eg.
$mem = memory_get_usage();
$DB_tmp = clone $DB;
$mem = memory_get_usage() - $mem;
unset($DB_tmp);

$mem could be the exact amount of memory allocated to $DB;

Answer:

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)
}

Answer:

Since clone (from Prof83’s answer) didn’t work for me, I tried to serialize and unserialize the variable whose size I want to measure:

function getMemoryUsage($var) {
    $mem = memory_get_usage();
    $tmp = unserialize(serialize($var));
    // Return the unserialized memory usage
    return memory_get_usage() - $mem;
}

I think it reports better results, at least for me.

Answer:

If you just want to know the size of object, and not for your next code, return it to the browser, and you can see how much the network transmit
the object.