It depends on the context because if it is misapplied cause opposite effect, increasing the use of memory and processing.
See a simple test:
Test 1
Here we use unset()
to remove variable indexes $arr
1 for 1.
The logic here is that we are already making an iteration in each of the indexes 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)
Chain: 10883880 (10mb)
Execution time (microseconds)
Total: 0.0090339183807373
Initial: 1469910070.9007
Ending: 1469910070.9097
Test 2
In this second test, we think of a different way. Starting from the logic that every time unset()
is invoked, would be consuming more memory and processing. As the ultimate goal is to completely erase the variable $arr
then we can do it by invoking unset() only 1 time, after repeat loop.
$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)
Chain: 4592344 (4mb)
Execution time (microseconds)
Total: 0.0061979293823242
Initial: 1469910142.5007
Ending: 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 function unset()
, within the context of the test, cause opposite effect, impairing performance significantly.
The running time presents a "negligible" difference of 0.003 micro seconds for human perception, however, it is a relevant difference in terms of data processing. But what draws attention the most is the memory consumption. The function unset()
can rather improve performance by freeing space in memory in significant way when used appropriately.
General rule:
- Repeated invocation of a function generates greater consumption.
- Applique
unset()
objects that will no longer be used, especially if there are still more routines to be performed. It 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 the context it is always good to free up space in memory.
PHP has a memory usage limiter, defined in the settings (php.ini). Normally the default is between 64mb and 128mb. A script that reaches half the processing already consuming 125mb, for example, has a great risk of causing execution interruption due to lack of memory. Many of these "out of memory" cases could be avoided if the script cleaned the variables that are not being used. Therefore, it is very valid to be careful to free up memory space during executions.
Terms used
peak: largest amount of space used during execution, i.e., the "memory peak"
current: amount of memory being used right after unset()
initial and final: the initial time and the final time of the execution in timestamp format, using the microtime function()
total: is the calculation of the difference between the initial time and the final time in microseconds.
Just remembering that the
unset()
is not a function, but a language constructor. See here the list.– stderr