It depends on the context because if it is misapplied, it causes the opposite effect, increasing the use of memory and processing.
See a simple test:
Test 1
Here we use unset()
to remove the indexes of the variable $arr
1 by 1.
The logic here is that we are already doing an iteration in each one of the indices so theoretically it could be more performative already to remove each one, right?
$arr = range(1, 100000);
foreach ($arr as $k => $v) {
if ($k % 2 === 0) {
$arr2[] = $v;
}
unset($arr[$k]);
}
The result of memory usage:
Pico : 17174288 (17mb)
Current : 10883880 (10mb)
Runtime (microseconds)
Total : 0.0090339183807373
Initial : 1469910070.9007
Final : 1469910070.9097
Test 2
In this second test, we think differently. Based on the logic that every time unset()
is invoked, it would be consuming more memory and processing. As the end goal is to completely delete the variable $arr
then we can do this by invoking unset () only once after the loop repetition.
$arr = range(1, 100000);
foreach ($arr as $k => $v) {
if ($k % 2 === 0) {
$arr2[] = $v;
}
}
unset($arr);
The result of memory usage:
Pico : 10882752 (10mb)
Current : 4592344 (4mb)
Runtime (microseconds)
Total : 0.0061979293823242
Initial : 1469910142.5007
Final : 1469910142.5069
Here we draw a conclusion that the theory of the second test is correct for the context presented here. Repeated invocation of the unset()
function, within the context of the test, causes the opposite effect, impairing performance significantly.
The runtime presents an "insignificant" difference of 0.003 micro seconds for human perception, however, it is a relevant difference in terms of data processing. But what most calls attention is the consumption of memory. The unset()
function can improve performance by releasing meaningful memory space when used properly.
General rule:
Repeated invocation of a function generates greater consumption.
Apply unset()
to objects that will no longer be used, especially if there are still more routines to execute. This will save memory for the rest of the processes.
Garbage Collector?
We can argue that it is "unnecessary" to be careful because at the end of all processes everything "dies", but depending on context it is always good to free up space in memory.
PHP has a memory usage limiter, defined in the settings (php.ini). Usually the default is between 64mb and 128mb. A script that comes in half of the processing already consuming 125mb, for example, has a great risk of causing interruption of execution due to lack of memory. Many of these out of memory cases could be avoided if the script cleans up variables that are not being used. Therefore, it is very valid to be careful in freeing the space in the memory during the executions.
Terms used
peak: greater amount of space used during execution, ie "peak memory"
current: amount of memory being used right after unset ()
start and end: the start time and end time of the execution in timestamp format, using the microtime ()
total: it is the calculation of the difference between the initial time and the final time, in microseconds.