echo 'Memory usage on script run:' .memory_get_usage() . "\n"; //let us create a simple object $var = array(); for($i=1; $i < 10000; $i++) { $var[] = 'Hello'; } //after array echo 'Memory usage when object is created:' .memory_get_usage() . "\n"; //memory usage is the same //because there was no data cloning, just pointing $new_var = $var; echo 'Memory usage when new pointer is made:' .memory_get_usage() . "\n"; //unset does not free resource //because there is still pointer to that memory location $var unset($var); echo 'Memory usage when one after uset:' .memory_get_usage() . "\n"; unset($new_var); echo 'Memory usage when one after all pointers:' .memory_get_usage() . "\n";
The code illustrates the problem. If you are using objects and you assign a variable an instance of an object it will hold the resource, because are actually pointing to that object and not cloning it. Therefor the resources used by an object will be collected by garbage collector only when all the pointers (variables) pointing to that resource are released.
Good article that explains garbage collectors can be found here
UPDATE: This problem has been solved by implementing the Concurrent Cycle Collection in Reference Counted Systems Algoritm