PHP performs garbage collection at three primary junctures:
- When you tell it to
- When you leave a function
- When the script ends
PHP garbage collection works on reference count.
unset your global variables as soon as you don’t need them. PHP keeps a reference count for all variables and destroys them (in most conditions) as soon as this reference count goes to zero. Objects have one internal reference count and the variables themselves (the object references) each have one reference count. When all the object references have been destroyed because their reference coutns have hit 0, the object itself will be destroyed.
But consider the following scenario:
These cyclic references are where PHP 5.3’s garbage collector kicks in. You can explicitly invoke the garbage collector with