php - what's the benefit of unsetting variables? [duplicate]
Possible Duplicates:
What's better at freeing memory with PHP: unset() or $var = null
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;
}
}
I开发者_如何学Gos 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?
Yep. From PHP.net:
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.
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.
Regarding your other question:
And is there any reason to unset variables apart from destroying session varaibles for instance or for scoping?
Not really, you pretty much summed it.
PHP will clean up memory on its own with the garbage collector, and it usually does a pretty good job. unsetting will simply make it explicit that you're done with that particular variable.
Probably no benefit for simple data types, but for any system resources you'd want to use that command to free those resources.
It depends on what the variable is. If it's a large array that consumes a few megs of data, and your script is liable to require lots of memory in the future (i.e.: before it finishes execution) then it would be wise to tag this memory as being available for use by unsetting the array.
That said, this is only really of use if the array is still in scope, as PHP will effectively have automatically disposed of it otherwise.
In terms of your provided example, there's no need to use unset, as those variables immediately go out of scope.
Even though there's no real gain over PHP's own garbage collection, I will occasionally unset()
variables to make it clear in the code that a var's role has been completed and will no longer be accessed or assigned. I tend not to do this with atomic data types, but instead with major actors in a script - configuration singletons, large objects, etc.
It releases memory which is being used by your script. See http://ie2.php.net/memory_get_usage.
The benefit is with scripts which are processing large amounts of data you can run into out of memory errors, see the memory_limit ini setting for more on this.
So, yes, there may be benefit, but unless you are working with large amounts of data you shouldn't need to use it.
You may also want to unset variable to prevent their value being used later on, but if that's the case it could be argued that your code needs to be written differently to prevent such things happening.
As mentioned in unset
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. 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.
精彩评论