Like this, for example:
class foo {}$foo = new foo();echo 123;...# 后面一直没有使用$foo这个对象,那么它是会被垃圾回收机制自动回收呢还是一直在内存中?
Reply content:
Like this, for example:
class foo {}$foo = new foo();echo 123;...# 后面一直没有使用$foo这个对象,那么它是会被垃圾回收机制自动回收呢还是一直在内存中?
PHP's GC is based on reference counting, so $foo will not be recycled and will remain in memory until the request is over.
Even if it is new, it is not assigned to $foo, and New Foo () is not necessarily a GC, and PHP's GC triggers are conditional.
Reference
PHP has a variable container to store all PHP variables, and then has a separate identifier to count the number of references to the variable, and when the number of references is 0, the variable is destroyed, which is equivalent to memory reclamation.
Logically you should call this a memory leak because you do not show the Go unset ($foo), which causes the reference count to always be 1! But PHP has a mechanism to prevent this kind of memory leaks, so your memory recycling here is done after the script has been executed.
Specific you can systematically look at this estimate will understand a lot of http://php.net/manual/zh/features.gc.php
I'm just reasoning:
It should be said in two steps that the new class should not cause memory operations, but the assignment actually causes memory allocations. So $ A = new class and $ A = 5; there is actually no difference unless explicitly deleted, otherwise it will persist until the end of the process is automatically reclaimed.
Understanding is not deep, roughly speaking.
object is not used after it is generated, it is a struct with a reference number of 1.
The struct reference number is 0 after leaving the scope, and the memory is reclaimed for the next automatic GC.
Should be automatically recycled, just as the declared object is not used, PHP has an automatic recovery mechanism