Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How important is it to unset variables in PHP?

Tags:

php

memory

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?

like image 217
dd0x Avatar asked Apr 11 '10 16:04

dd0x


People also ask

What is the use of unset in PHP?

The unset() function in PHP resets any variable. If unset() is called inside a user-defined function, it unsets the local variables. If a user wants to unset the global variable inside the function, then he/she has to use $GLOBALS array to do so.

Is unset variable used in PHP?

PHP | unset() Function The unset() function is an inbuilt function in PHP which is used to unset a specified variable.

What's better at freeing memory with PHP unset () or $var Null?

null variable immediately frees the memory.

What is the use of unset?

Definition and Usage The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.


2 Answers

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 unsetting $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.

like image 104
Gordon Avatar answered Oct 03 '22 00:10

Gordon


There are many situations in which unset will not actually deallocate much of anything, so its use is generally quite pointless unless the logical flow of your code necessitates its non-existence.

like image 31
Azeem.Butt Avatar answered Oct 03 '22 00:10

Azeem.Butt