Circular references and PHP's memory management
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
CoughPHP |
Confirmed
|
Undecided
|
Richard Pistole | ||
1.4 |
New
|
Undecided
|
Unassigned |
Bug Description
When loading objects and collections that are related to each other, Cough automatically backlinks certain things to others to keep resource usage to a minimum, rather than duplicating the object in memory for all instances where they link up... Example:
foreach($collection as $element) {
}
However, if you happen to be running a loop of things, and instantiating a collection into the same variable name over and over, i.e.:
foreach(
));
// do some other stuff with $someOther_
}
Obviously, this is bad coding practice, but is done in some situations nonetheless... The problem is that PHP's memory management won't reclaim any of the memory used in this situation, when the $someOther_
Garbage collection only reclaims memory when there are no outstanding references to it, so even if you explicitly unset($
The solution is simple, add destructors to both the CoughObject and CoughCollection classes to recursively unset all references in their objects and collections members, like so:
abstract class CoughObject {
public function __destruct() {
}
}
}
}
abstract class CoughCollection extends ArrayObject {
public function __destruct() {
}
}
}
The destructors will fire off anytime a variable goes out of scope, is overwritten, or is explicitly unset(), and will recursively destroy their way down to the bottom of any other nested objects or collections, allowing that memory to be reclaimed by the system... This is PHP bug #33595 (http://
I was talking with Velveeta on IRC about this, I'm going to get this into my branch when I have the time. Seems like a relatively quick/decent fix to a problem with the way PHP's GC is broken with orphaned object graphs.