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.