用PHP释放内存的更好的方法是:unset()或$ var = null [英] What's better at freeing memory with PHP: unset() or $var = null

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

问题描述

我意识到第二个方法避免了函数调用的开销( update ,实际上是一种语言构造),但是知道一个方法是否比另一个更好会很有趣.我的大多数编码都一直使用unset(),但是最近我浏览了一些使用$var = null代替的在网上发现的受人尊敬的类.

有没有首选的人,这是什么原因?

解决方案

unset手册页请勿不再包含该部分了

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


问题"未设置和= null之间的差异"详述了一些差异:


unset($a)还将符号表中的$a删除;例如:

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

输出:

 Notice: Undefined variable: a in xxx
NULL
 

但是使用$a = null时:

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

NULL
 

$a = null似乎比其unset()同行要快一点:更新符号表条目似乎比删除它快.


  • 当您尝试使用不存在的(unset)变量时,将触发错误,并且变量表达式的值将为null. (因为,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天全站免登陆