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

发布于 2024-08-03 06:03:33 字数 137 浏览 3 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晌融 2024-08-10 06:03:33

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

编辑:

为了澄清有关存储分配的内存大小的部分,您可以执行以下操作:

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

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

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.

梦晓ヶ微光ヅ倾城 2024-08-10 06:03:33

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

例如

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

只是一个想法?

另一个混乱但可能准确的想法:

假设一个类实例变量自实例化以来已经被操作了几次:

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

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

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;

一身骄傲 2024-08-10 06:03:33

我认为这不太可能;我从未见过任何可以让您获取内存中对象的大小的东西。

获得一个相当粗略的想法的解决方案可能是序列化您的数据,并对其使用 strlen...但这实际上是一种估计...实际上,我不太依赖这样的东西。 ..


即使 debug_zval_dump 也不不要这样做:它输出变量和引用计数中的数据,但不输出使用的内存:

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

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

只会让你:

object(stdClass)#1 (2) refcount(2){
  ["a"]=>
  long(152) refcount(1)
  ["b"]=>
  string(4) "test" refcount(1)
}

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)
}
摘星┃星的人 2024-08-10 06:03:33

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

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

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

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.

江南月 2024-08-10 06:03:33

如果您只想知道对象的大小,而不是为了您的下一个代码,请将其返回给浏览器,您可以看到网络传输了多少
物体。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文