Using unset in PHP to destroy variables and release memory

Source: Internet
Author: User
The unset () function of PHP is used to clear and destroy variables. unused variables can be destroyed using unset. However, in some cases, the memory occupied by destroying variables cannot be reached with unset! The code is as follows:
For ($ I = 1; I I <100; $ I ++ ){
$ Str = str_repeat ('20140901', $ I );
$ A = memory_get_usage ();
Unset ($ str );
$ B = memory_get_usage ();
Echo "\ n
". $ I. ':'. ($ B-$ a). 'bytes .';
}

The result shows that:
8x32 = 256 it is really necessary to release the memory when it is 256 bytes long. some people say that it is not as fast as $ str = null.
The result is 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.
32:-272 Bytes.
33:-280 Bytes.
34:-288 Bytes.
35:-296 Bytes.
36:-304 Bytes.
37:-312 Bytes.
38:-320 Bytes.
39:-328 Bytes.
40:-336 Bytes.
41:-344 Bytes.
42:-352 Bytes.
43:-360 Bytes.
44:-368 Bytes.
45:-376 Bytes.
46:-384 Bytes.
47:-392 Bytes.
48:-400 Bytes.
49:-408 Bytes.
50:-416 Bytes.
51:-424 Bytes.
52:-432 Bytes.
53:-440 Bytes.
54:-448 Bytes.
55:-456 Bytes.
56:-464 Bytes.
57:-472 Bytes.
58:-480 Bytes.
59:-488 Bytes.
60:-496 Bytes.
61:-504 Bytes.
62:-512 Bytes.
63:-520 Bytes.
64:-528 Bytes.
65:-536 Bytes.
66:-544 Bytes.
67:-552 Bytes.
68:-560 Bytes.
69:-568 Bytes.
70:-576 Bytes.
71:-584 Bytes.
72:-592 Bytes.
73:-600 Bytes.
74:-608 Bytes.
75:-616 Bytes.
76:-624 Bytes.
77:-632 Bytes.
78:-640 Bytes.
79:-648 Bytes.
80:-656 Bytes.
81:-664 Bytes.
82:-672 Bytes.
83:-680 Bytes.
84:-688 Bytes.
85:-696 Bytes.
86:-704 Bytes.
87:-712 Bytes.
88:- 720 Bytes.
89:-728 Bytes.
90:-736 Bytes.
91:-744 Bytes.
92:- 752 Bytes.
93:- 760 Bytes.
94:-768 Bytes.
95:-776 Bytes.
96:-784 Bytes.
97:-792 Bytes.
98:-800 Bytes.
99:-808 Bytes.

Let's first look at an example.
The code is as follows:
$ S = str_repeat ('1', 255); // generates a string consisting of 255 1
$ M = memory_get_usage (); // Get the memory occupied currently
Unset ($ s );
$ Mm = memory_get_usage (); // unset () and check the memory occupied.
Echo $ m-$ mm;
?>

Finally, the output unset () occupies memory minus unset () and then occupies memory. if it is a positive number, it indicates that unset ($ s) has destroyed $ s from memory (or, after unset (), the memory usage is reduced. However, on the PHP5 and windows platforms, the result is-48. Does this indicate that unset ($ s) does not destroy the memory occupied by the variable $ s? Let's take the following example:
The code is as follows:
$ S = str_repeat ('1', 256); // generates a string consisting of 256 1
$ M = memory_get_usage (); // Get the memory occupied currently
Unset ($ s );
$ Mm = memory_get_usage (); // unset () and check the memory occupied.
Echo $ m-$ mm;
?>

In this example, it is almost the same as the preceding example. The only difference is that $ s is composed of 256 1 s, that is, one more than the first example. The result is 224. Does this indicate that unset ($ s) has destroyed the memory occupied by $ s?
Through the above two examples, we can draw the following conclusions: Conclusion 1: The unset () function can only release the memory space when the variable value occupies more than 256 bytes of memory.
Is it possible to use unset to release memory space as long as the variable value exceeds 256? Let's test it through an example:
The 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); // destroy $ s
$ Mm = memory_get_usage ();
Echo $ p .'
';
Echo $ m-$ mm;
?>

'Refresh the page. we can see that there are 256 1 in the first line and-48 in the second line. we have destroyed $ s, while $ p only references $ s variables, there should be no content. In addition, the memory usage after unset ($ s) is higher than that before unset! Now let's take the following example:
The 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;
?>

Refresh the page and we can see that the output $ p has no content. The difference between memory usage and unset () is 224, that is, the memory occupied by variables has been cleared. In this example, $ s = null can also be replaced with unset (), as follows:
The 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); // destroy $ s
Unset ($ p );
$ Mm = memory_get_usage ();
Echo $ p .'
';
Echo $ m-$ mm;
?>

We will use unset () to destroy both $ s and $ p. then we can see that the memory usage difference is also 224, which means the memory can be released. Therefore, we can draw another conclusion: Conclusion 2: The memory will be released only when all the variables pointing to this variable (such as the referenced variables) are destroyed.
I believe that after the example in this article, you should have some knowledge about unset (). at the very least, I use unset () to release the memory when the variable does not work.
Related Article

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.