php 如何在PHP中查找对象使用的内存?(大小)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1075419/
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 00:52:24  来源:igfitidea点击:

How to find memory used by an object in PHP? (sizeof)

phpmemorymemory-management

提问by erotsppa

How to find memory used by an object in PHP? (c's sizeof). The object I want to find out about is a dictionary with strings and ints in it so it makes it hard to calculate it manually. Also string in php can be of varied length depending on encoding (utf8 etc) correct?

如何在PHP中查找对象使用的内存?(c 的大小)。我想了解的对象是一个包含字符串和整数的字典,因此很难手动计算它。php 中的字符串也可以根据编码(utf8 等)的不同而具有不同的长度,是否正确?

回答by zombat

You could use memory_get_usage().

您可以使用memory_get_usage()

Run it once before creating your object, then again after creating your object, and take the difference between the two results.

在创建对象之前运行一次,然后在创建对象后再次运行,并计算两个结果之间的差异。

回答by rekowski

To get an idea about the objects size, try

要了解对象大小,请尝试

strlen(serialize($object));

It is by no means accurate, but an easy way to get a number for comparison.

这绝不是准确的,而是一种获取数字进行比较的简单方法。

回答by LionsAd

If you need to know the size of an already created object or array, you can use the following code to find it out.

如果需要知道已经创建的对象或数组的大小,可以使用下面的代码来查找。

<?php

function rec_copy($src) {
  if (is_string($src)) {
    return str_replace('SOME_NEVER_OCCURING_VALUE_145645645734534523', 'XYZ', $src);
  }

  if (is_numeric($src)) {
    return ($src + 0);
  }

  if (is_bool($src)) {
    return ($src?TRUE:FALSE);
  }
  if (is_null($src)) {
    return NULL;
  }

  if (is_object($src)) {
    $new = (object) array();
    foreach ($src as $key => $val) {
      $new->$key = rec_copy($val);
    }
    return $new;
  }

  if (!is_array($src)) {
    print_r(gettype($src) . "\n");
    return $src;
  }

  $new = array();

  foreach ($src as $key => $val) {
    $new[$key] = rec_copy($val);
  }
  return $new;
}

$old = memory_get_usage();
$dummy = rec_copy($src);
$mem = memory_get_usage();

$size = abs($mem - $old);
?>

This essentially creates a copy of the array structure andall of its members.

这实质上创建了数组结构及其所有成员的副本。

A not 100% accurate, but still working version is also:

一个不是 100% 准确但仍然有效的版本也是:

<?php

$old = memory_get_usage();
$dummy = unserialize(serialize($src));
$mem = memory_get_usage();

$size = abs($mem - $old);

Hope that helps for cases where the object is already build.

希望对已经构建对象的情况有所帮助。

回答by Peter

I don't know that there is a simple way to get the size of an object in PHP. You might just have to do an algorith that

我不知道有一种简单的方法可以在 PHP 中获取对象的大小。你可能只需要做一个算法

  1. Counts the ints
  2. Multiplies number of ints by size of an int on hard disk
  3. Convert characters in strings to ASCII and
  4. Multiply the ASCII values by how much they take up on disk
  1. 计算整数
  2. 将整数数乘以硬盘上整数的大小
  3. 将字符串中的字符转换为 ASCII 和
  4. 将 ASCII 值乘以它们在磁盘上的占用量

I'm sure there is a better way, but this would work, even though it would be a pain.

我相信有更好的方法,但这会奏效,即使这会很痛苦。