Replace wrapTransversable generators to prevent memory leaks#2709
Merged
GromNaN merged 4 commits intodoctrine:2.9.xfrom Dec 19, 2024
Merged
Replace wrapTransversable generators to prevent memory leaks#2709GromNaN merged 4 commits intodoctrine:2.9.xfrom
wrapTransversable generators to prevent memory leaks#2709GromNaN merged 4 commits intodoctrine:2.9.xfrom
Conversation
GromNaN
commented
Dec 19, 2024
|
|
||
| private bool $iteratorAdvanced = false; | ||
|
|
||
| private bool $iteratorExhausted = false; |
Member
Author
There was a problem hiding this comment.
Replaced by setting $iterator = null, as it's already done for UnrewindableIterator
| $this->storeCurrentItem(); | ||
| $this->iterator = new IteratorIterator($iterator); | ||
| $this->iterator->rewind(); | ||
| if ($this->iterator->valid()) { |
Member
Author
There was a problem hiding this comment.
This checks for a dead cursor.
alcaeus
reviewed
Dec 19, 2024
Member
alcaeus
left a comment
There was a problem hiding this comment.
Great work refactoring this. I've left a suggestion to refactor storeCurrentItems to consolidate some logic, but LGTM aside from that.
tests/Doctrine/ODM/MongoDB/Tests/Functional/Iterator/UnrewindableIteratorTest.php
Outdated
Show resolved
Hide resolved
alcaeus
approved these changes
Dec 19, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
A non-static generator created with a method of a class instance holds the reference to this instance. When assigned to a property of this instance, this creates a circular reference (instance -> generator -> instance). The reference is destroyed only when the generator is exhausted.
Most of the time, when the memory limit is sufficient, this is not an issue. The garbage collector should remove this circular references, but it runs automatically only when reaching the
GC_THRESHOLD_DEFAULT.When working with a MongoDB Cursor, this is a single object which can fill up a lot of memory, that explains why
gc_collect_cycles()is not run automatically.Solution
Using
IteratorIteratorinstead ofGenerator, we remove this circular reference . Also, I find the code simpler.Same as mongodb/mongo-php-library#694
Demonstration
In the following example, if
gc_collect_cycles()is not called in the loop, we can see the memory leak. When callinggc_collect_cycles()explicitly, we can see 300 "collected" references. The memory leak disappears when theGeneratoris replaced by anIteratorIterator.Source code