Memory management and garbage collection for PHP scripts

Source: Internet
Author: User

This article to you to share about the PHP script memory management and garbage collection, through the example to show you the PHP script memory management and garbage collection, the need for friends can refer to.

Reference assignment

$a = ' Apple '; $b = & $a;

In the code above, I assign a string to the variable a, and then assign a reference to the variable B. Obviously, this time the memory point should be like this:

$a ' apple ' <-$b

A and B point to the same chunk of memory (the variable container) that we var_dump($a, $b) get string(5) "apple" string(5) "apple" , which is the result we expect.

Reference count vs. unset ()

If I want to 'apple' release this string from memory. That's what I did:

unset ($a);

But by printing the $a $b Two variable information again, I got the result: Notice: Undefined variable: a and string(5) "apple" . Strange, $a $b pointing at the same time an area of memory, and clearly will be $a released, why $b or 'apple' .

In fact, unset() this is a variable pointer is destroyed, and does not release the memory area (variable container), so after the operation, the memory point just become:

' Apple ' <-$b

The key to keep in mind: unset () does not release the block of memory that the variable points to (the variable container), but only destroys the variable pointer. At the same time, the reference count (ref count) of that memory is reduced by 1, and when the reference count is 0 o'clock, that is, when the block of memory (the variable container) is not referenced by any variable, PHP garbage collection is triggered.

Use code to verify:

$a = ' Apple '; $b = & $a; $before = Memory_get_usage (); unset ($a); $after = Memory_get_usage (); Var_dump ($before-$after);  //result is int (0), no release
$a = ' Apple '; $b = & $a; $before = Memory_get_usage (); unset ($a, $b); $after = Memory_get_usage (); Var_dump ($before-$afte R);  The result is an int (24), which gets released

Direct Recycling

What can be done to really release 'apple' the memory that is occupied?

Using the above method, we can then unset($a) unset($b) destroy all references in the memory area, reduce the reference count to 0, and naturally be recycled by PHP.

There is, of course, a more straightforward approach:

$a = null;

The direct assignment null will $a empty the area of memory pointed to, and the reference count will be zeroed, and the memory will be freed.

End of script execution

PHP is a scripting language, and when the script executes, all memory used within the script is freed. So, do we manually release the meaning of the inside? Actually about this question, early has the answer, recommended everybody to look at the bird Brother @laruence an article published in 2012: Please release your resource manually (PLS release resources manually)

Reference assignment

$a = ' Apple '; $b = & $a;

In the code above, I assign a string to the variable a, and then assign a reference to the variable B. Obviously, this time the memory point should be like this:

$a ' apple ' <-$b

A and B point to the same chunk of memory (the variable container) that we var_dump($a, $b) get string(5) "apple" string(5) "apple" , which is the result we expect.

Reference count vs. unset ()

If I want to 'apple' release this string from memory. That's what I did:

unset ($a);

But by printing the $a $b Two variable information again, I got the result: Notice: Undefined variable: a and string(5) "apple" . Strange, $a $b pointing at the same time an area of memory, and clearly will be $a released, why $b or 'apple' .

In fact, unset() this is a variable pointer is destroyed, and does not release the memory area (variable container), so after the operation, the memory point just become:

' Apple ' <-$b

The key to keep in mind: unset () does not release the block of memory that the variable points to (the variable container), but only destroys the variable pointer. At the same time, the reference count (ref count) of that memory is reduced by 1, and when the reference count is 0 o'clock, that is, when the block of memory (the variable container) is not referenced by any variable, PHP garbage collection is triggered.

Use code to verify:

$a = ' Apple '; $b = & $a; $before = Memory_get_usage (); unset ($a); $after = Memory_get_usage (); Var_dump ($before-$after);  //result is int (0), no release
$a = ' Apple '; $b = & $a; $before = Memory_get_usage (); unset ($a, $b); $after = Memory_get_usage (); Var_dump ($before-$afte R);  The result is an int (24), which gets released

Direct Recycling

What can be done to really release 'apple' the memory that is occupied?

Using the above method, we can then unset($a) unset($b) destroy all references in the memory area, reduce the reference count to 0, and naturally be recycled by PHP.

There is, of course, a more straightforward approach:

$a = null;

The direct assignment null will $a empty the area of memory pointed to, and the reference count will be zeroed, and the memory will be freed.

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.