1. How to check the current memory
Use memory_get_usage() function to view memory
2. Experiment
<?php
function aa()
{
var_dump(memory_get_usage(false)); //int(22356464)
for ($q=1;$q<1000;$q++){
$a[] = $q;
}
var_dump(memory_get_usage(false)); //int(22393384)
array_pop($a);
var_dump(memory_get_usage(false)); //int(22393408)
$b = $a;
var_dump(memory_get_usage(false)); //int(22393408)
unset($a);
var_dump(memory_get_usage(false)); //int(22393384)
unset($b);
var_dump(memory_get_usage(false)); //int(22356464)
}
function dd()
{
$this->aa();
var_dump('last:'.memory_get_usage(false)); // last:22356464
}
You can see that this experiment is a method that calls another method to test the memory changes after the variable is unset.
- The initial memory value is 22356464
- 2 Create an array $a and fill it with 1000 values. At this time, the memory reaches 22393384, and the memory increases by 36860.
- We remove a value in the array and look at the memory: 22393408, the memory has increased by 24.
- Then assign $a to $b, then look at the memory: 22393408, there is no change in the memory.
- After unset($a), the memory falls back to 22393384, a decrease of 24.
- Then unset($b) again, this is the memory falling back to the initial value of 22356464
- In the return dd() function, the memory is still the initial value 22356464
From this experiment, we can see that if $a has been assigned to other variables, when we unset the original variable $a, it will not release its original memory. In fact, when we assign $a to $b, we can find that the memory does not double the memory of $a. In fact, $a and $b are equivalent to labels, pointing to this pile of data. When we unset it In fact, $a just removes the "label" and does not point to this pile of data. If this pile of data is not labeled, it will be released. If there is such a $b that also points to this pile of data, it will not be released. .
So unset() can release memory, provided that this variable is not assigned to another variable