-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[NFR] Events & Weak References #658
Comments
|
Garbage collector copes poorly with only cyclical references: class A
{
public $b;
}
class B {
public $a;
}
$a = new A;
$b = new B;
$a->b = $b;
$b->a = $a;
unset($a, $b); // memory still leaks But you may always call |
I know how GC working. gc_collect_cycles() - it's other. 1) You unlinked $myListener variable of MyListener object, but $eventsManager still keeps this reference In first way i suggest always use WeakRef in Event\Manager without strong references. So in this case after unset($myListener) Event\Manager must always lose references to $myListener. |
What will happen with $myListener if it was initialized inside some method after it is executed? call_user_func(function() use($di)
{
$myListener = new MyListener();
$di->get('eventsManager')->attachUsingWeakRef('somecomponent', $myListener);
}); Should the GC deallocate $myListener? |
@Agent-J In this case - yes. Because after your Closure will be executed the $myListener will not have any strong references anymore. And all event-handlers provided by this listener will be "unatached" from EventsManager. |
So you will be able to attach the listener only in the global scope or ongoing call stack otherwise the reference will be lost. |
And what about this case? $eventsManager->attachUsingWeakRef('somecomponent', new MyListener()); |
I sent a simple pull. You can try how it works :) |
No! nowadays, no! |
@sasezaki You're right! To this point has already been removed this functionality from zend, because there it used by default. So when weakref was installed or wasn't installed, this was causing the different behavior of logic of zf2. I not suggest using by default, i suggest add a possibility to use weakref. And who don't know how it works will not use this, but who know how it works can use this (and will able to achieve an increase in productivity and a significant reduction in memory usage) |
Implemented in 1.3.0 |
Coool! Thank you! |
Weak references provide a non-intrusive gateway to ephemeral objects. Unlike normal (strong) references, weak references do not prevent the garbage collector from freeing that object. For this reason, an object may be destroyed even though a weak reference to that object still exists. In such conditions, the weak reference seamlessly becomes invalid.
I suggest to use Weak References with Events (with Listeners).
Base idea:
I have three suggests (must be selected only one):
1.Always uses WeakRef for all event-handlers
p.s. But i don't like this solution, because we need strong references in some cases.
2.Add a flag/method for use or not weak references.
3.Use weakref-php-extension and add possibility register Event-handler as "WeakRef" object.
So when event fired then will checked that event-handler is WeakRef object or not.
If is WeakRef object then will check whether the object referenced still exists and if exists - fire!
If is not WeakRef object then standart fire.
Avaliable php-extension for Weak References: http://www.php.net/manual/en/book.weakref.php
Zend Framework 2 uses WeakRef extension.
The text was updated successfully, but these errors were encountered: