在PHP中取消设置变量有多重要? [英] How important is it to unset variables in PHP?
问题描述
我对PHP有点陌生,我想知道:
在PHP中取消设置变量有多重要?
我知道像C这样的语言,我们释放了分配的内存以防止泄漏等.通过在使用完变量后使用unset变量,这会大大提高我的应用程序性能吗?
还可以在任何地方比较使用未设置和不使用未设置之间的区别的基准吗?
$x = str_repeat('x', 80000);
echo memory_get_usage() . "<br>\n"; // 120172
echo memory_get_peak_usage() . "<br>\n"; // 121248
$x = str_repeat('x', 80000);
echo memory_get_usage() . "<br>\n"; // 120172
echo memory_get_peak_usage() . "<br>\n"; // 201284
正如您所看到的,在某一时刻,PHP几乎消耗了两倍的内存.这是因为在将'x'字符串分配给$x
之前,PHP会在内存中构建新的字符串,同时也在内存中保留前一个变量.可以通过unset
ting $x
来防止这种情况.
另一个例子:
for ($i=0; $i<3; $i++) {
$str = str_repeat("Hello", 10000);
echo memory_get_peak_usage(), PHP_EOL;
}
这将输出类似
375696
425824
425824
在第一次迭代中,$str
在分配之前仍然为空.在第二次迭代中,$str
将保留生成的字符串.当第二次调用str_repeat
时,它不会立即覆盖$str
,而是首先创建要在内存中分配的字符串.因此,您最终得到$str
及其应分配的值.双重记忆.如果您取消设置$str
,则不会发生:
for($i=0;$i<3;$i++) {
$str = str_repeat("Hello", 10000);
echo memory_get_peak_usage(), PHP_EOL;
unset($str);
}
// outputs something like
375904
376016
376016
有关系吗?好吧,链接的文章总结
很好这不是关键,除非有必要.
在不再需要变量时取消设置变量没有什么坏处.也许您在共享主机上,并且想要对大型数据集进行一些迭代.如果取消设置会阻止PHP以允许的XXXX字节的内存大小耗尽结尾,则值得付出很小的努力.
还应考虑的是,即使请求生存期仅是一秒钟,将内存使用量增加一倍也可以将可同时处理的最大请求数量减半.如果您仍然无法达到服务器的限制,那么谁在乎,但是如果您不在,那么简单的设置就可以为您节省更多RAM或额外服务器的费用.
I am somewhat new to PHP, and I am wondering:
How important is it to unset variables in PHP?
I know in languages like C, we free the allocated memory to prevent leaks, etc. By using unset on variables when I am done with them, will this significantly increase performance of my applications?
Also, is there a benchmark anywhere that compares the difference between using unset and not using unset?
See this example (and the article I linked below the question):
$x = str_repeat('x', 80000);
echo memory_get_usage() . "<br>\n"; // 120172
echo memory_get_peak_usage() . "<br>\n"; // 121248
$x = str_repeat('x', 80000);
echo memory_get_usage() . "<br>\n"; // 120172
echo memory_get_peak_usage() . "<br>\n"; // 201284
As you can see, at one point PHP had used up almost double the memory. This is because before assigning the 'x'-string to $x
, PHP builds the new string in memory, while holding the previous variable in memory, too. This could have been prevented with unset
ting $x
.
Another example:
for ($i=0; $i<3; $i++) {
$str = str_repeat("Hello", 10000);
echo memory_get_peak_usage(), PHP_EOL;
}
This will output something like
375696
425824
425824
At the first iteration $str
is still empty before assignment. On the second iteration $str
will hold the generated string though. When str_repeat
is then called for the second time, it will not immediately overwrite $str
, but first create the string that is to be assigned in memory. So you end up with $str
and the value it should be assigned. Double memory. If you unset $str
, this will not happen:
for($i=0;$i<3;$i++) {
$str = str_repeat("Hello", 10000);
echo memory_get_peak_usage(), PHP_EOL;
unset($str);
}
// outputs something like
375904
376016
376016
Does it matter? Well, the linked article sums it quite good with
This isn't critical, except when it is.
It doesn't hurt to unset your variables when you no longer need them. Maybe you are on a shared host and want to do some iterating over large datasets. If unsetting would prevent PHP from ending with Allowed memory size of XXXX bytes exhausted, then it's worth the tiny effort.
What should also be taken into account is, that even if the request lifetime is just a second, doubling the memory usage effectively halves the maximum amount of simultaneous requests that can be served. If you are nowhere close to the server's limit anyway, then who cares, but if you are, then a simple unset could save you the money for more RAM or an additional server.
这篇关于在PHP中取消设置变量有多重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!