PHP-取消设置变量有什么好处? [英] php - what's the benefit of unsetting variables?

查看:67
本文介绍了PHP-取消设置变量有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复项:
使用PHP释放内存的更好方法是:unset( )或$ var = null

Possible Duplicates:
What's better at freeing memory with PHP: unset() or $var = null

在php中设置变量真的有好处吗?

Is there a real benefit of unsetting variables in php?

class test {

  public function m1($a, $b)
    $c = $a + $b;
    unset($a, $b);
    return $c;
  }
}

设置变量确实没有减少运行时的内存消耗吗?

Is it true that unsetting variables doesn't actually decrease the memory consumption during runtime?

推荐答案

设置变量是否为真 并没有真正减少内存 在运行时消耗?

Is it true that unsetting variables doesn't actually decrease the memory consumption during runtime?

.来自 PHP.net :

unset()就像它的名字所说的那样 -取消设置变量.它不会强制立即释放内存. PHP的 垃圾收集器会在它做的时候做 看到适合-有意向,尽快 无论如何都不需要这些CPU周期, 或直到脚本将 内存不足,无论发生什么情况 首先.

unset() does just what it's 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.

如果您正在执行$ whatever = null; 然后您要重写变量的 数据.您可能会释放内存/ 缩小速度更快,但可能会窃取CPU 从真正需要的代码开始循环 他们更快,导致更长的时间 总执行时间.

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.

关于您的其他问题:

还有什么理由要取消设置 除了破坏之外的变量 会话变量,例如或 范围?

And is there any reason to unset variables apart from destroying session varaibles for instance or for scoping?

不是真的,您几乎可以总结一下.

这篇关于PHP-取消设置变量有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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