Getting size in memory of an object in PHP?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 02:03:20

问题


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.


回答1:


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.




回答2:


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;




回答3:


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



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/1351855/getting-size-in-memory-of-an-object-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!