在 PHP 中获取对象的内存大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1351855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:08:54  来源:igfitidea点击:

Getting size in memory of an object in PHP?

phpoopobjectsize

提问by Artem Russakovskii

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?

有一种方法可以获取 PHP 使用的总内存 ( memory_get_usage()) 但是如何获取单个对象的内存大小?

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

我显然不是在谈论,count()因为我想要潜在复杂数据结构中的字节数。

采纳答案by Eric J.

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.

您可以在分配类之前和之后调用 memory_get_usage(),如 IBM 的这个示例中所示。您甚至可以创建一个包装器来执行此操作,可能会将结果存储在复杂类本身的成员变量中。

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.

在未来的任何时候,您都可以检查 allocationSize 以查看该对象在分配时有多大。但是,如果您在分配之后添加它,那么 allocationSize 将不再准确。

回答by Prof83

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???

尝试序列化对象并读取字符串长度没有意义吗?显然它会减少几个字节,因为序列化的字符串会有 s:'string' 因此 s:'' 是额外的字节......除非序列化可以与 PHP 存储对象的方式相同???

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;

$mem 可能是分配给 $DB 的确切内存量;

回答by Pascal MARTIN

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...

获得一个非常粗略的想法的解决方案可能是序列化您的数据,并在其上使用 strlen ......但这确实是一种估计......实际上,我不会完全依赖于这样的东西。 ..



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

甚至debug_zval_dump不这样做:它将数据输出到变量和引用计数中,但不输出使用的内存:

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

回答by Iván Pérez

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:

由于克隆(来自Prof83 的回答)对我不起作用,我尝试序列化和反序列化我想要测量其大小的变量:

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.

我认为它报告了更好的结果,至少对我而言。

回答by haofly

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

如果你只是想知道对象的大小,而不是为你的下一个代码,return它给浏览器,你可以看到网络传输了多少对象。