PHP unset destroys variables and frees memory

Source: Internet
Author: User

The PHP unset () function is used to clear, destroy variables, and we can destroy them with unset (). But at some point, using unset () does not achieve the memory that is consumed by destroying variables! Let's look at an example first:

<? PHP $s=str_repeat// generates a string consisting of 255 1 $m// gets the current occupied memory  unset($s); $mm // unset () and then view the current memory consumption Echo $m-$mm;? >

The

Last output unset () consumes memory minus unset () before it takes up the memory, and if it is a positive number, then unset ($s) has destroyed the $s from memory (or, unset () has decreased memory footprint), 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's make the following example

<? PHP $s=str_repeat// generates a string consisting of 256 1 $m// gets the current occupied memory  unset($s); $mm // 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:

<?PHP$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.‘ <br/> ';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 refers to $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:

<?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 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:

<?PHP$s=str_repeat(' 1 ', 256);//This is exactly the same as the second example .$p=&$s;$m=memory_get_usage ();unset($s);//destroying $sunset($p);$mm=memory_get_usage ();Echo $p.‘ <br/> ';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.

PHP unset destroys variables and frees memory

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.