Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Avoid Doctrine Memory Leaks

Boynux
January 22, 2014

Avoid Doctrine Memory Leaks

How to avoid Doctrine memory leaks and increase application performance.

Boynux

January 22, 2014
Tweet

More Decks by Boynux

Other Decks in Programming

Transcript

  1. Avoid DOCTRINE Memory Leakage How to avoid memory leaks in

    long running processe that uses Doctrine!
  2. The Problem • Doctrine keeps reference to any entity. •

    As PHP 5.2.5 Doctrine does not GC graph objects • Don't fetch extra data from database in one-many relations • Fetch what you need! • In most HTTP served pages it might not be serious. • How ever in long running processes it leads to huge memory leaks •
  3. Avoid references • Always `detach` entity from UOW when you

    don't need to update that. $entityManger->detach ($entity);
  4. GC Graph Objects • Use `free` to release recursive references

    from Doctrine objects to let PHP to GC the remaining objects. • The reason is objects have reference to their parents and children recursively. $query = $this->_em->createQuery ('…'); $result = $query->getResult (); $query->free ();
  5. Don't Fetch Extra Data • This is for one-to-many and

    many-to-many relations • Assume Post entity has one-to-many relation to comments • And naturally comments have many-to-one relation to Posts $comment = new Comment ('My Comment'); $post->comments[] = $comment; $post->save (); INSTEAD –----------------------------- $comment = new Comment ('My Comment'); $comment->post_id = $post_id; $comment->save ();
  6. Fetch what you need! • Don't use: $item = $blah->find

    ($id); • Instead query the columns you need: $query = Doctrine\Query::create() ->select ('b.col1, b.col2, b.col3') ->from ('blah b') ->where ('b.id = ?') ->orderBy ('b.col2 DESC'); $item = $query->execute (array ($id));
  7. Thank You! • References: – Doctrine web site (http://doctrine-project.org) –

    Stackoverflow (http://stackoverflow.com) – My experiments (http://www.boynux.com) 2014-01-22