Last Updated: February 25, 2016
·
1.156K
· nexik

Doctrine Huge Collections Performance Tip

Doctrine 1.x have a lot of performance problem. In last year I was forced to use it and had a lot problems.

If you need to write big tasks or migrations with tons of doctrine object you can try to utilize php garbage collector.

First try

foreach ($collection as $object) {
  // some code
  unset($object);
}

Unfortunately object have circular references, and garbage collector cannot handle this.

Proper way

foreach ($collection as $object) {
  // some code
  $object->free(true);
  unset($object);
}

Doctrine objects, collections and query have free method. If you pass true, method will recursively free all references. Now php garbage collector work fine and even huge synchronize task can be exeuted in one run.