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:

    1. $s=str_repeat(' 1 ', 255); //Generate a string consisting of 255 1
    2. $m=memory_get_usage (); //Get current memory consumption
    3. unset ($s);
    4. $mm=memory_get_usage (); //unset () and then view the current memory consumption
    5. Echo $m-$mm;
    6. ?>

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:

    1. $s=str_repeat(' 1 ', 256); //Generate a string consisting of 256 1
    2. $m=memory_get_usage (); //Get current memory consumption
    3. unset ($s);
    4. $mm=memory_get_usage (); //unset () and then view the current memory consumption
    5. Echo $m-$mm;
    6. ?>

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:

    1. $s=str_repeat(' 1 ', 256); //This is exactly the same as the second example
    2. $p=&$s;
    3. $m=memory_get_usage ();
    4. unset ($s); //Destroy $s
    5. $mm=memory_get_usage ();
    6. Echo $p. '
      ';
    7. Echo $m-$mm;
    8. ?>

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:

    1. $s=str_repeat(' 1 ', 256); //This is exactly the same as the second example
    2. $p=&$s;
    3. $m=memory_get_usage ();
    4. $s=null; //Set $s to null
    5. $mm=memory_get_usage ();
    6. Echo $p. '
      ';
    7. Echo $m-$mm;
    8. ?>

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:

    1. $s=str_repeat(' 1 ', 256); //This is exactly the same as the second example
    2. $p=&$s;
    3. $m=memory_get_usage ();
    4. unset ($s); //Destroy $s
    5. unset ($p);
    6. $mm=memory_get_usage ();
    7. Echo $p. '
      ';
    8. Echo $m-$mm;
    9. ?>

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.

The above describes the PHP unset destroy variables and free memory, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.