Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDC-3146: Hydrator memory leak when using iterator #3925

Closed
doctrinebot opened this issue May 30, 2014 · 6 comments
Closed

DDC-3146: Hydrator memory leak when using iterator #3925

doctrinebot opened this issue May 30, 2014 · 6 comments
Assignees
Labels
Milestone

Comments

@doctrinebot
Copy link

Jira issue originally created by user Emiel:

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.

$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<ins></ins>) {
    // 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.

@doctrinebot
Copy link
Author

Comment created by boonedox:

I'm experiencing the same problem on 2.3.2

@doctrinebot
Copy link
Author

Comment created by boonedox:

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

@doctrinebot
Copy link
Author

Comment created by @Ocramius:

[~boonedox] can you provide a PR with the fix and a test?

@coodix
Copy link

coodix commented Dec 22, 2015

+1

1 similar comment
@kpeu3i
Copy link

kpeu3i commented May 17, 2016

+1

@Ocramius
Copy link
Member

Handled in #1515

@Ocramius Ocramius added this to the 2.5.7 milestone Jun 24, 2017
@Ocramius Ocramius assigned Ocramius and unassigned beberlei Jun 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants