30

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;
  }
}

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

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
n00b
  • 16,088
  • 21
  • 56
  • 72
  • in php 5.3 you have a garbage collector so you can ignore this, but when you run big script in php 5.2 and use big array, you can find yourself deal with memory leak problems, so you may be aware to unset variables – Haim Evgi Feb 17 '11 at 15:04
  • Possible Duplicate: http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null – Phill Pafford Feb 17 '11 at 15:04
  • Might also want to read this: http://www.tuxradar.com/practicalphp/18/1/10 – Phill Pafford Feb 17 '11 at 15:05
  • 4
    This question certainly overlaps with the mentioned question, but the title of the question is actually pretty specific and very likely to be searched for by others. I wouldn't say this is a duplicate. – Aron Rotteveel Feb 17 '11 at 15:09
  • alright it's close to that other question but definitely(!) no duplicate :) – n00b Feb 17 '11 at 15:16
  • 2
    Both questions came up in my search, and I purposely first clicked on this one since it was what I was looking for. The "duplicate" apparently did not have this strong first impression (for me at least). – Ben Jan 17 '13 at 04:58

7 Answers7

37

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.

Aron Rotteveel
  • 81,193
  • 17
  • 104
  • 128
  • I'm guessing the mentioned other part to the question has been edited out, and perhaps a missing comma is causing me to misinterpret it, but there are quite a few other reasons why you might want to unset a variable that extends beyond removing just _session_ data within certain scopes. In general, its purpose to make intention clear and prevent use of the variable any further. Picture a sever where two sites use the same codebase, but different indexes. A programmer would have to look between both indexes to find out which variables can/should safely be used elsewhere in the codebase. – Super Cat Jul 30 '18 at 05:46
  • And while I realize there are human readable comments, naming conventions, and other methods to manually limit scope (classes, blocks), principle stands and access to unwanted or localized data should not be made available to the rest of the program, and it's not always possible or practical to limit its scope. I'd say it's perfectly OK to remove the `unset()` at a production level, as long as the data you're passing to output is within its own output scope. – Super Cat Jul 30 '18 at 05:50
5

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500
3

Probably no benefit for simple data types, but for any system resources you'd want to use that command to free those resources.

Achilles
  • 11,165
  • 9
  • 62
  • 113
3

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.

John Parker
  • 54,048
  • 11
  • 129
  • 129
2

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.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • 1
    You can also use a comment to do it, e.g. `// unset foobar`. This has the benefit that no code is run at all. – Pacerier Sep 02 '15 at 08:10
1

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.

David Gillen
  • 1,172
  • 5
  • 14
0

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.

Vadiklk
  • 3,696
  • 5
  • 28
  • 44
  • Can you share me an example (or related topics) of this conclusion? I just want to understand 'it may steal CPU cycles from the code that truly needs them sooner' more.. – Vuong Mar 12 '18 at 09:39