What's better at freeing memory with unset() or $var = 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?
Verified Answer (233 Votes) ✓
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);
Notice: Undefined variable: a in xxx NULL
$a = nullis used:
$a = str_repeat('hello world ', 100); $a = null; var_dump($a); Outputs: NULL
It seems that
$a = nullis 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.
Answer #2 (48 Votes)
unset is not actually a function, but a language construct. It is no more a function call than a
return or an
Aside from performance issues, using
unset makes your code's intent much clearer.
Answer #3 (35 Votes)
By doing an unset() on a variable, you've essentially marked the variable for 'garbage collection' (PHP doesn't really have one, but for example's sake) so the memory isn't immediately available. The variable no longer houses the data, but the stack remains at the larger size. Doing the null method drops the data and shrinks the stack memory almost immediately.
This has been from personal experience and others as well. See the comments of the unset() function here.
I personally use unset() between iterations in a loop so that I don't have to have the delay of the stack being yo-yo'd in size. The data is gone, but the footprint remains. On the next iteration, the memory is already being taken by php and thus, quicker to initialize the next variable.