存储一个整数的实际内存成本是多少


What is the actual memory cost of storing an integer?

假设我只是做这个

$arr = array();
for ($i = 0; $i < 10; $i++) $arr[] = $i;

所以我把10个整数存储在一个数组中。如果整数是32b的整数,那么内存成本应该是40字节。问题是,我没有告诉php它是一个整数,所以它要么必须将它存储为其他东西?(例如,js喜欢做双打)或者保留额外的数据来提醒自己它是什么?这10个数字+数组在系统内存中实际占用了多少内存?

这很重要,因为我正在尝试评估我想移植到php的算法的可行性,而且它有点占用内存。

您的问题很难回答,因为确切的内存占用取决于几个因素,我将在下面简要介绍其中的一些因素。

如果你只需要一些快速数字,可以考虑:

  • memory_get_usage—返回分配给PHP的内存量
  • memory_get_peak_usage—返回PHP分配的内存峰值

PHP将值内部存储在一个名为zval:的结构中

121 struct _zval_struct {
122    zend_value        value;            /* value */
123    union {
124        struct {
125            ZEND_ENDIAN_LOHI_4(
126                zend_uchar    type,         /* active type */
127                zend_uchar    type_flags,
128                zend_uchar    const_flags,
129                zend_uchar    reserved)     /* call info for EX(This) */
130        } v;
131        uint32_t type_info;
132    } u1;
133    union {
134        uint32_t     var_flags;
135        uint32_t     next;                 /* hash collision chain */
136        uint32_t     cache_slot;           /* literal cache slot */
137        uint32_t     lineno;               /* line number (for ast nodes) */
138        uint32_t     num_args;             /* arguments number for EX(This) */
139        uint32_t     fe_pos;               /* foreach position */
140        uint32_t     fe_iter_idx;          /* foreach iterator index */
141    } u2;
142};

这是一个并集类型,意味着它可以存储多种类型的值。所以,是的,它保留了额外的数据来提醒自己它是什么。整数通常表示为long(32或64位,取决于您的平台)。

关于阵列,NikiC有一篇优秀的博客文章给出了详细的解释。要点:

                              |  64 bit   | 32 bit
---------------------------------------------------
zval                         |  24 bytes | 16 bytes
+ cyclic GC info             |   8 bytes |  4 bytes
+ allocation header          |  16 bytes |  8 bytes
===================================================
zval (value) total           |  48 bytes | 28 bytes
===================================================
bucket                       |  72 bytes | 36 bytes
+ allocation header          |  16 bytes |  8 bytes
+ pointer                    |   8 bytes |  4 bytes
===================================================
bucket (array element) total |  96 bytes | 48 bytes
===================================================
total total                  | 144 bytes | 76 bytes

以上数字将根据您的操作系统、编译器和编译选项而有所不同。例如,如果你使用调试或线程安全编译PHP,你会得到不同的数字。但我认为上面给出的大小是您将在Linux上PHP 5.3的平均64位生产版本中看到的。

同样值得注意的是,非数字数组键标签也是一种分配,因此标签越长,消耗的内存就越多。

上面的数字还取决于您使用的PHP版本。PHP的内存占用量>=7应该比任何PHP<7由于Zend引擎的内部变化。有关详细信息,请参阅以下两篇博客文章:

  • https://nikic.github.io/2015/05/05/Internal-value-representation-in-PHP-7-part-1.html
  • https://nikic.github.io/2015/06/19/Internal-value-representation-in-PHP-7-part-2.html

另请参阅这些资源了解更多信息:

  • http://php.net/manual/en/internals2.variables.intro.php
  • http://blog.ircmaxell.com/2012/03/phps-source-code-for-php-developers_21.html