As of version 5.2.5, PHP is not able to garbage collect object graphs that have circular references, e.g. Parent has a reference to Child which has a reference to Parent. Since many doctrine model objects have such relations, PHP will not free their memory even when the objects go out of scope.
For most PHP applications, this problem is of little consequence, since PHP scripts tend to be short-lived. Longer-lived scripts, e.g. bulk data importers and exporters, can run out of memory unless you manually break the circular reference chains. Doctrine provides a free() function on Doctrine_Record, Doctrine_Collection, and Doctrine_Query which eliminates the circular references on those objects, freeing them up for garbage collection. Usage might look like:
Free objects when mass inserting records:
for ($i = 0; $i < 1000; $i++)
{
$object = createBigObject();
$object->save();
$object->free(true);
}
You can also free query objects in the same way:
for ($i = 0; $i < 1000; $i++)
{
$q = Doctrine_Query::create()
->from('User u');
$results = $q->fetchArray();
$q->free();
}
Or even better if you can reuse the same query object for each query in the loop that would be ideal:
$q = Doctrine_Query::create()
->from('User u');
for ($i = 0; $i < 1000; $i++)
{
$results = $q->fetchArray();
$q->free();
}