Using unset to destroy variables and memory release problems in PHP _php tutorial

Source: Internet
Author: User
Copy CodeThe code is 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. ';
}

As seen from the results:
8 x 32 = 256 It is really necessary to release the memory at 256 bytes long, and some people say that it is not 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.
Bytes: -288.
: -296 Bytes.
Bytes: -304.
Panax Notoginseng: -312 Bytes.
: -320 Bytes.
: -328 Bytes.
Max: -336 Bytes.
In: -344 Bytes.
: -352 Bytes.
Bytes: -360.
: -368 Bytes.
: -376 Bytes.
Bytes: -384.
Bytes: -392.
Bytes: -400.
$ -408 Bytes.
: -416 Bytes.
Wuyi: -424 Bytes.
Bytes: -432.
: -440 Bytes.
Wu: -448 Bytes.
: -456 Bytes.
Bytes: -464.
£ º -472 Bytes.
: -480 Bytes.
Bytes: -488.
: -496 Bytes.
Bytes: -504.
: -512 Bytes.
: -520 Bytes.
: -528 Bytes.
: -536 Bytes.
: -544 Bytes.
Bytes: -552.
Bytes: -560.
Bytes: -568.
: -576 Bytes.
Bytes: -584.
Bytes: -592.
: -600 Bytes.
Bytes: -608.
Bytes: -616.
Bytes: -624.
Bytes: -632.
: -640 Bytes.
Bytes: -648.
Bytes: -656.
Bayi: -664 Bytes.
Bytes: -672.
Bytes: -680.
Bytes: -688.
Bytes: -696.
Bytes: -704.
: -712 Bytes.
: -720 Bytes.
Bytes: -728.
: -736 Bytes.
Bytes: -744.
: -752 Bytes.
Bytes: -760.
94: -768 Bytes.
Bytes: -776.
: -784 Bytes.
$ -792 Bytes.
98: -800 Bytes.
: -808 Bytes.

Let's look at an example first.
Copy CodeThe code is as follows:
$s =str_repeat (' 1 ', 255); Produces a string consisting of 255 1
$m =memory_get_usage (); Get Current memory consumption
Unset ($s);
$MM =memory_get_usage (); Unset () and then view the current memory consumption
echo $m-$mm;
?>

After the last output unset () takes up memory minus unset () before it takes up memory, if it is a positive number, then unset ($s) has destroyed $s from memory (or, after unset () the memory footprint is reduced), but I am under PHP5 and Windows platform, The resulting result is:-48. Does this mean that unset ($s) does not play the role of destroying the memory that the variable $s consumes? Let us make the following example:
Copy CodeThe code is as follows:
$s =str_repeat (' 1 ', 256); Produces a string consisting of 256 1
$m =memory_get_usage (); Get Current memory consumption
Unset ($s);
$MM =memory_get_usage (); Unset () and then view the current memory consumption
echo $m-$mm;
?>

This example is almost identical to the example above, the only difference being that the $s consists of 256 1, which 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 conclude that the first, unset () function frees up memory space only if the value of the variable takes up more than 256 bytes of memory.
So is it possible to use unset to free up memory space as long as the value of the variable exceeds 256? Let's test it with one more example:
Copy CodeThe code is as follows:
$s =str_repeat (' 1 ', 256); This is exactly the same as the second example.
$p =& $s;
$m =memory_get_usage ();
Unset ($s); Destroying $s
$MM =memory_get_usage ();
echo $p. '
';
echo $m-$mm;
?>

' Refresh the page, we see the first line has 256 1, the second row is-48, supposedly we have destroyed the $s, and $p just reference $s variables, should be no content, in addition, unset ($s) after the memory consumption than unset () increased before! Now let's do the following example:
Copy CodeThe code is as follows:
$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. '
';
echo $m-$mm;
?>

Now refresh the page, we see that the output $p has no content, unset () before and after the memory consumption of the difference is 224, that has cleared the memory of the variable occupied. The $s=null in this example can also be replaced with unset (), as follows:
Copy CodeThe code is as follows:
$s =str_repeat (' 1 ', 256); This is exactly the same as the second example.
$p =& $s;
$m =memory_get_usage ();
Unset ($s); Destroying $s
Unset ($p);
$MM =memory_get_usage ();
echo $p. '
';
echo $m-$mm;
?>

We destroy both $s and $p using unset (), and then the difference between memory consumption is 224, which means that memory can also be freed. So, we can get another conclusion: two, only if all variables that point to the variable, such as reference variables, are destroyed, the memory is freed.
I believe that after the example of this article, we should have some understanding of unset (), at least, I use unset () is also in order to free the memory when the variable does not work.

http://www.bkjia.com/PHPjc/325687.html www.bkjia.com true http://www.bkjia.com/PHPjc/325687.html techarticle Copy the 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-...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.