php – 存储整数的实际内存开销是多少?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 存储整数的实际内存开销是多少?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想说我就是这样做的

$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分配的内存峰值

PHPstructure called the 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位,具体取决于您的平台).

至于阵列,有一个excellent blog post by 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. PHP> = 7的内存占用量应远低于任何PHP< 7由于Zend Engine的内部更改.有关详细信息,请参阅这两篇博文:
> 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

脚本宝典总结

以上是脚本宝典为你收集整理的php – 存储整数的实际内存开销是多少?全部内容,希望文章能够帮你解决php – 存储整数的实际内存开销是多少?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。