Doctrine 2 - ORM
  1. Doctrine 2 - ORM
  2. DDC-3146

Hydrator memory leak when using iterator

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.3.2, 2.3.5, 2.4.2
    • Fix Version/s: None
    • Component/s: ORM
    • Security Level: All
    • Labels:
    • Environment:
      PHP 5.4.21, OS X 10.9.3; PHP 5.3.28, OS X 10.9.4

      Description

      When the hydrator iterate() function is invoked, a new event is added to the event manager with a reference to the current hydrator object. This reference is never cleared which causes the hydrator object to never be cleared from memory by the PHP garbage collection.

      /doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php
      $evm = $this->_em->getEventManager();
      $evm->addEventListener(array(Events::onClear), $this);
      

      The effects of this bug are best visible when creating multiple iterator objects after each other in a repository:

      // Loop through test code 10 times
      for ($f = 0; $f < 10; $f++) {
          // Create test query
          $query = $this->createQueryBuilder('p')->getQuery();
      
          // Create IterableResult object
          $iterableResult = $query->iterate();
      
          // Loop through the iterator
          foreach ($iterableResult as $row) {}
      
          // Print out memory usage
          print(memory_get_usage() . PHP_EOL);
      }
      

      This results in the following output:

      10536552
      10549920
      10563288
      10576664
      10590040
      10603416
      10616792
      10630168
      10643608
      10656984
      

      Notice how the used memory increases by about 13KB after each iteration.
      To stop this memory leak the following code can be added to the end of the cleanup() function in the AbstractHydrator class:

      $evm = $this->_em->getEventManager();
      $evm->removeEventListener(array(Events::onClear), $this);
      

      The output is now:

      10537920
      10537048
      10537048
      10537048
      10537048
      10537048
      10537048
      10537048
      10537048
      10537048
      

      The reference to the event manager is now automatically removed in the cleanup function which allows the hydrator object to be cleaned up by the garbage collection function in PHP.

        Activity

        Hide
        Jeremiah Johnson added a comment -

        I'm experiencing the same problem on 2.3.2

        Show
        Jeremiah Johnson added a comment - I'm experiencing the same problem on 2.3.2
        Hide
        Jeremiah Johnson added a comment -

        Tested latest of 2.3.x (2.3.5) and problem still exists.

        Show
        Jeremiah Johnson added a comment - Tested latest of 2.3.x (2.3.5) and problem still exists.
        Hide
        Marco Pivetta added a comment -

        Jeremiah Johnson can you provide a PR with the fix and a test?

        Show
        Marco Pivetta added a comment - Jeremiah Johnson can you provide a PR with the fix and a test?

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Emiel Nijpels
          • Votes:
            1 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated: