[6.x] Optimize eager loading memory handling #30248
Merged
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.
Fixes #30114
This pull request fixes a memory leak issue reported in the mentioned issue,
I think this may be a bug in PHP itself so I will report to them too.The problem is that the Eloquent builder is creating closures for the relations, these relations are stored in the eagerLoads property of the class because these closures are containing a reference to the builder the GC thinks the class is still referenced. By refactoring these closures to static ones this reference does not get stored so the GC can correctly clean up the builder earlier.
edit: Turns out this just delays the GC, it gets detected later when memory usage hits ~24MB. Updated the title to optimize.
These methods does not use the
$this
reference so there is no other change needed.