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

Asked
Active3 hr before
Viewed126 times

8 Answers

better
90%

Meta Stack Overflow ,Stack Overflow em Português,Thanks for contributing an answer to Stack Overflow!,I created a new performance test for unset and =null, because as mentioned in the comments the here written has an error (the recreating of the elements). I used arrays, as you see it didn't matter now.

unset($a) also removes $a from the symbol table; for example:

$a = str_repeat('hello world ', 100);
unset($a);
var_dump($a);

Outputs:

Notice: Undefined variable: a in xxx
NULL

But when $a = null is used:

$a = str_repeat('hello world ', 100);
$a = null;
var_dump($a);

Outputs:

NULL
load more v
88%

unset() does not force immediate memory freeing, and it is used to free variable usage.,PHP garbage collector cleans up the unset variables.,null variable immediately frees the memory.,In this article, we will discuss freeing the memory with unset() and using NULL value to any variable.

Syntax:

unset($variable)

Output:

Before unset: hello geeks
After unset:

Syntax:

$variable = null;

Output:

Before null: Hello geeks
After null:
load more v
72%

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.,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.,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.,It seems that $a = null is a bit faster than its unset() counterpart: updating a symbol table entry appears to be faster than removing it.

unset($a) also removes $a from the symbol table; for example:

$a = str_repeat('hello world ', 100);
unset($a);
var_dump($a);
load more v
65%

The unset() function of PHP is used to clear and destroy variables. We can use unset() to destroy unused variables. But sometimes, unset() can’t destroy the memory occupied by variables!,PHP uses unset() to delete a unit in the array,Conclusion 1. The unset() function can only release the memory space when the variable value occupies more than 256 bytes.,Does this mean that unset ($s) does not destroy the memory occupied by variable $s? Let’s take the following example:

< ? php
$s = str_ Repeat('1 ', 255); // generates a string of 255 ones
$m = memory_ get_ Usage(); // get the current occupied memory
unset($s);
$mm = memory_ get_ After usage(); // unset(), check the current memory usage
echo $m - $mm; ?
>
load more v
75%

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. , To unset() a global variable inside of a function, then use the $GLOBALS array to do so: , If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. , If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

bar
load more v
40%

Freeing memory with PHP I... , Graphics Programming Using GCC ... ,Face your challenges with a -deep determination, PHP Coding TipsMake your code more beautiful and o...


        <
        ? php
        $ignore = array('GLOBALS', '_FILES', '_COOKIE', '_POST', '_GET', '_SERVER', '_ENV', 'argv', 'argc', 'ignore');
        $definedVariablesArr = array_diff_key(get_defined_vars() + array_flip($ignore), array_flip($ignore));
        foreach($definedVariablesArr AS $var) {
           $ {
              $var
           } = NULL;
        }
        echo memory_get_usage(); ?
        >
load more v
22%

Possible Duplicates: What's better at freeing memory with PHP: unset() or $var = null ,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.,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.,Is it true that unsetting variables doesn't actually decrease the memory consumption during runtime?

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

Also I've just come to add unsets when dealing with memory intensive code all around just as a precaution for things that maybe to come.,Hey all, I'm trying to learn as much as I can about PHP and the best programming practices for it. I never use PHP's unset() function to unset a variable, because I've never learned if I should or not. Is it generally recommended to unset all variables when they are done use? Do you use unset() in your projects?,It seems to me that as long as you maintain good code style, 99% of the time it is quite sufficient to handle memory management by scope. Keep your functions, short and sweet.,It helps with memory usage if you intend to do a lot of things after you unset() a variable.

You imply that unsettling a variable is different than just letting it lose scope. Is this actually the case? For example if I do the following:

$f = fopen('file');
$g = $f;
unset($f);

Other "better-undefined" queries related to "What's better at freeing memory with PHP: unset() or $var = null"