Copy Code code as follows:
for ($i = 1; $i < $i + +) {
$str = str_repeat (' 01234567 ', $i);
$a = Memory_get_usage ();
Unset ($STR);
$b = Memory_get_usage ();
echo "\ n
". $i. ': '. ($b-$a). ' Bytes. ';
}
From the result, we see:
8 x 32 = 256 It is really necessary to free up memory at 256 bytes Long, some people say, as fast as direct $STR = null.
The results are as follows:
1:0 Bytes.
2:0 Bytes.
3:0 Bytes.
4:0 Bytes.
5:0 Bytes.
6:0 Bytes.
7:0 Bytes.
8:0 Bytes.
9:0 Bytes.
10:0 Bytes.
11:0 Bytes.
12:0 Bytes.
13:0 Bytes.
14:0 Bytes.
15:0 Bytes.
16:0 Bytes.
17:0 Bytes.
18:0 Bytes.
19:0 Bytes.
20:0 Bytes.
21:0 Bytes.
22:0 Bytes.
23:0 Bytes.
24:0 Bytes.
25:0 Bytes.
26:0 Bytes.
27:0 Bytes.
28:0 Bytes.
29:0 Bytes.
30:0 Bytes.
31:0 Bytes.
-272 Bytes.
-280 Bytes.
-288 Bytes.
-296: Bytes.
-304 Bytes.
Panax Notoginseng: -312 Bytes.
-320 Bytes.
-328: Bytes.
-336: Bytes.
-344 Bytes.
-352 Bytes.
-360 Bytes.
-368: Bytes.
-376: Bytes.
-384: Bytes.
-392 Bytes.
-400 Bytes.
-408: Bytes.
-416: Bytes.
Wuyi: -424 Bytes.
-432 Bytes.
-440 Bytes.
Si: -448 Bytes.
-456: Bytes.
-464 Bytes.
-472 Bytes.
-480 Bytes.
-488: Bytes.
-496: Bytes.
-504 Bytes.
-512 Bytes.
-520 Bytes.
£ -528 Bytes.
-536 Bytes.
Bytes: -544.
-552 Bytes.
-560 Bytes.
-568 Bytes.
-576: Bytes.
-584 Bytes.
-592 Bytes.
-600 Bytes.
Bytes: -608.
-616 Bytes.
Bytes: -624.
-632 Bytes.
-640 Bytes.
-648 Bytes.
-656: Bytes.
Bayi: -664 Bytes.
-672 Bytes.
-680 Bytes.
-688 Bytes.
-696: Bytes.
-704: Bytes.
-712 Bytes.
-720: Bytes.
-728: Bytes.
-736: Bytes.
-744 Bytes.
-752 Bytes.
M: -760 Bytes.
: -768 Bytes.
-776: Bytes.
-784 Bytes.
Bytes: -792.
-800 Bytes.
-808: Bytes.
Let's look at an example first.
Copy Code code as follows:
<?php
$s =str_repeat (' 1 ', 255); Produces a string consisting of 255 1
$m =memory_get_usage (); Get current memory footprint
Unset ($s);
$MM =memory_get_usage (); Unset () and then view current memory consumption
echo $m-$mm;
?>
After the final output unset () consumes memory minus unset () and if it is positive, then unset ($s) has destroyed $s from memory (or, after unset () the memory footprint is reduced), but I am under PHP5 and Windows platforms, The results are:-48. Does this indicate that unset ($s) does not play a role in destroying the memory occupied by the variable $s? Let us make the following example:
Copy Code code as follows:
<?php
$s =str_repeat (' 1 ', 256); Produces a string consisting of 256 1
$m =memory_get_usage (); Get current memory footprint
Unset ($s);
$MM =memory_get_usage (); Unset () and then view current memory consumption
echo $m-$mm;
?>
This example is almost the same as the example above, the only difference being that the $s is made up of 256 1, that is, a 1 more than the first example, and the result is: 224. Does this indicate that unset ($s) has destroyed the memory occupied by $s?
With the above two examples, we can draw the following conclusions: conclusion one, unset () function frees up memory space only if the variable value occupies more than 256 bytes of memory space.
So as long as the value of the variable is more than 256, you can use unset to free up memory space? Let's test it again with an example:
Copy Code code as follows:
<?php
$s =str_repeat (' 1 ', 256); This is exactly the same as the second example.
$p =& $s;
$m =memory_get_usage ();
Unset ($s); Destroy $s
$MM =memory_get_usage ();
echo $p. ' <br/> ';
echo $m-$mm;
?>
' Refresh the page, we see the first row has 256 1, the second line is-48, we have destroyed the $s, and $p just reference $s variables, there should be no content, in addition, unset ($s) after the memory footprint than the unset () before the increase! Now let's take the following example:
Copy Code code as follows:
<?php
$s =str_repeat (' 1 ', 256); This is exactly the same as the second example.
$p =& $s;
$m =memory_get_usage ();
$s =null; Set $s to null
$MM =memory_get_usage ();
echo $p. ' <br/> ';
echo $m-$mm;
?>
Now refresh the page, we see that the output $p has no content, unset () before and after the difference of memory footprint is 224, that is, the variable occupied memory has been cleared. The $s=null in this example can also be replaced by unset (), as follows:
Copy Code code as follows:
<?php
$s =str_repeat (' 1 ', 256); This is exactly the same as the second example.
$p =& $s;
$m =memory_get_usage ();
Unset ($s); Destroy $s
Unset ($p);
$MM =memory_get_usage ();
echo $p. ' <br/> ';
echo $m-$mm;
?>
We use unset () to destroy both $s and $p, and then the difference between the memory footprint is also 224, which means that memory can also be freed. So, we can get another conclusion: Second, the memory is freed only when all variables (such as reference variables) that point to the variable are destroyed.
Believe that after the example of this article, we should have an understanding of unset (), at the very least, I use unset () is to release the memory when the variable does not work.