-
Notifications
You must be signed in to change notification settings - Fork 64
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
$_cache variable significantly increases memory usage #25
Comments
I think we will solve issue by other (__destructor()) way. Thanks for catch. |
unfortunately __destruct() is only called when the script ends. Thus PHP's garbage collection is not triggered to free up the memory. the only way is to manually remove the circular reference by calling detachBehavior(). |
Yes. But i'm against to override detach(). Framework should call destructor on detachBehavior() if behavior have it. So its framework issue. Working on this. |
@creocoder what are you trying to do about it? PHP should be aware of cycle references but as mentioned in yiisoft/yii#1329 (comment) there seems to be a bug in php. |
@cebe So solution is: foreach ($models as $model) {
$model->detachBehaviors();
}
gc_collect_cycles(); ? And we should not do anything? |
I'm having a huge memory leak issue when iterating through large numbers of ActiveRecords with NestedSetBehavior attached. This is because of the circular reference that is created behaviors are added to ActiveRecord (see yiisoft/yii#1329).
To test this issue:
Create 500 objects with NestedSetBehavior attached and log the memory usage before and after.
The results:
The Problem:
I found out that the $_cache variable used in the afterConstruct() and afterFind() methods were causing a huge increase in memory usage and are not released even after unsetting $model due to circular referencing.
Solution:
Add this function to NestedSetBehavior:
public function detach($owner)
{
// free up memory when detachBehavior() is called.
self::$_cached = null;
self::$_c = null;
}
Call $model->detachBehaviors();
Results:
Before: 8.75MB
After: 12MB
The text was updated successfully, but these errors were encountered: