用 PHP 释放内存有什么好处:unset() 或 $var = null [英] What's better at freeing memory with PHP: unset() or $var = null

查看:31
本文介绍了用 PHP 释放内存有什么好处:unset() 或 $var = null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到第二个避免了函数调用的开销(update,实际上是一种语言构造),但是知道一个是否比另一个更好会很有趣.我一直在使用 unset() 进行大部分编码,但我最近查看了一些从网上找到的使用 $var = null 的受人尊敬的类.

有没有首选的,理由是什么?

解决方案

在 2009 年取消设置手册页面:

<块引用>

unset() 就像它的名字所说的那样 - 取消设置一个变量.它不会强制立即释放内存.PHP 的垃圾收集器会在它认为合适的时候执行它 - 出于意图,因为无论如何都不需要这些 CPU 周期,或者直到脚本耗尽内存之前,无论先发生什么.

如果你在做 $whatever = null; 那么你正在重写变量的数据.您可能会更快地释放/缩小内存,但它可能会更快地从真正需要它们的代码中窃取 CPU 周期,从而导致更长的整体执行时间.

(自 2013 年起,unset 手册页 不再包含该部分)

注意直到php5.3,如果你有循环引用中的两个对象,例如在父子关系中,在父对象上调用 unset() 不会释放子对象中用于父引用的内存.(当父对象被垃圾回收时,内存也不会被释放.)(bug 33595)


问题unset 和 = null 之间的区别"详细说明一些差异:


unset($a) 也会从符号表中删除 $a;例如:

$a = str_repeat('hello world', 100);未设置($a);var_dump($a);

<块引用>

输出:

注意:未定义变量:xxx中的a空值

<块引用>

但是当使用 $a = null 时:

$a = str_repeat('hello world', 100);$a = 空;var_dump($a);

<块引用>

输出:

NULL

<块引用>

似乎 $a = null 比对应的 unset() 快一点:更新符号表条目似乎比删除它快.>


  • 当您尝试使用不存在的 (unset) 变量时,将触发错误并且变量表达式的值将为空.(因为,PHP 还应该做什么?每个表达式都需要产生某个值.)
  • 分配了 null 的变量仍然是一个完全正常的变量.

I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is better than the other. I have been using unset() for most of my coding, but I've recently looked through a few respectable classes found off the net that use $var = null instead.

Is there a preferred one, and what is the reasoning?

解决方案

It was mentioned in the unset manual's page in 2009:

unset() does just what its name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

(Since 2013, that unset man page don't include that section anymore)

Note that until php5.3, if you have two objects in circular reference, such as in a parent-child relationship, calling unset() on the parent object will not free the memory used for the parent reference in the child object. (Nor will the memory be freed when the parent object is garbage-collected.) (bug 33595)


The question "difference between unset and = null" details some differences:


unset($a) also removes $a from the symbol table; for example:

$a = str_repeat('hello world ', 100);
unset($a);
var_dump($a);

Outputs:

Notice: Undefined variable: a in xxx
NULL

But when $a = null is used:

$a = str_repeat('hello world ', 100);
$a = null;
var_dump($a);

Outputs:

NULL

It seems that $a = null is a bit faster than its unset() counterpart: updating a symbol table entry appears to be faster than removing it.


  • when you try to use a non-existent (unset) variable, an error will be triggered and the value for the variable expression will be null. (Because, what else should PHP do? Every expression needs to result in some value.)
  • A variable with null assigned to it is still a perfectly normal variable though.

这篇关于用 PHP 释放内存有什么好处:unset() 或 $var = null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆