gpt4 book ai didi

php - 为什么引用使用更多内存?

转载 作者:搜寻专家 更新时间:2023-10-31 21:12:44 24 4
gpt4 key购买 nike

他们不应该让脚本使用更少的内存吗?

function a(&$var); 使用的内存比 function a($var);

foreach($array as $k => &$v) 使用的内存比 foreach($array as $k => $v)

例如 $var 使用 24 MB。如果我通过引用传递它,它使用 27 MB

最佳答案

(不幸的是)无法详细解释 zend 引擎内部的魔法,我指的是 documentation :

References in PHP are a means to access the same variable content by different names. They are not like C pointers; for instance, you cannot perform pointer arithmetic using them, they are not actual memory addresses, and so on. See What References Are Not for more information. Instead, they are symbol table aliases. Note that in PHP, variable name and variable content are different, so the same content can have different names. The closest analogy is with Unix filenames and files - variable names are directory entries, while variable content is the file itself. References can be likened to hardlinking in Unix filesystem.

我觉得和unix文件系统比较就很容易理解了。如果一个文件有多个名称,则需要额外的内存来存储这些名称。

关于php - 为什么引用使用更多内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15650753/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com