For example: {code...} is like this:
Class foo {} $ foo = new foo (); echo 123 ;... # The $ foo object is not used in the future. Will it be automatically recycled by the garbage collection mechanism or will it be in the memory?
Reply content:
For example:
Class foo {} $ foo = new foo (); echo 123 ;... # The $ foo object is not used in the future. Will it be automatically recycled by the garbage collection mechanism or will it be in the memory?
Php gc is based on reference count, so $ foo will not be recycled and will remain in the memory until the request ends.
Even if it is only new, $ foo is not assigned, and new foo () is not necessarily gc. The gc trigger of php is conditional.
Reference
PHP has a variable container to store all PHP variables, and then has a separate identifier to count the number of variable references. When the number of references is 0, the variable is destroyed, which is equivalent to memory recycling.
Logically, this should be called a memory leak, because you do not display unset ($ foo), resulting in the reference count is always 1! However, PHP has a mechanism to prevent such Memory leakage. Therefore, the memory reclaim here is performed after the script is executed.
You can look at the specific system, this estimate will understand a lot of http://php.net/manual/zh/features.gc.php
I am just reasoning:
It should be said in two steps that the new Class should not trigger memory operations, but the value assignment actually causes memory allocation. So $ a = new Class and $ a = 5; there is actually no difference. Unless you delete it explicitly, it will continue until the process ends and be automatically recycled.
I am not very familiar with it.
If the object is not used after it is generated, it will be a struct with a reference number of 1.
After leaving the scope, the number of struct references is 0, and the memory will be reclaimed when the next automatic GC is performed.
It should be automatically recycled, just as the declared object is not used, php has an automatic recycle Mechanism