Reprinted from: http://www.okajax.com/a/201106/php_unset.html
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! Let's first look at an example.
<?PHP$ S=Str_repeat('1', 255 );//Generate a string consisting of 255 1$ M= Memory_get_usage ();//Obtains the memory occupied currently.Unset($ S);$ Mm= Memory_get_usage ();//Unset () and then check the memory occupied currentlyEcho $ M-$ Mm;?>
memory occupied before the final output unset () minus unset (). If it is a positive number, unset ($ S) $ S has been destroyed from the memory (or the memory usage is reduced after unset (), but on 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 use the following example:
<?PHP$ S=Str_repeat('1', 256 );//Generate a string consisting of 256 1$ M= Memory_get_usage ();//Obtains the memory occupied currently.Unset($ S);$ Mm= Memory_get_usage ();//Unset () and then check the memory occupied currentlyEcho $ 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 only releases the memory space when the memory occupied by the variable value exceeds 256 bytes.
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:
<? 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 and we can see that there are 256 1 in the first line and-48 in the second line. We have destroyed $ S, and $ p just references the $ s variable, 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:
<? 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 ; ?>
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:
<? 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 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. Then, we can draw another conclusion:Conclusion 2: the memory is released only when all variables pointing to the variable (such as 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.