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
Document Expando to mention weak references #16172
Comments
Removed Type-Defect label. |
Could you use Expando for the things you want weak references for? |
This comment was originally written by @arhad Well, I see. But Expando documentation is poor. It even is not clear that this class is a set of properties, automatically destroyed on binded object destroy. So there is no mention that Expando may be used as a weak pointers array to data, accessible via "[]" operator. |
This comment was originally written by @arhad Reassign this enchancement to documentation area, please. |
We should improve the docmentation for Expando. I assume the dart2js version of Expando can leak memory, since it doesn't use proper weak pointers (yet). As far as I can see, discarding the expando does not clear the values stored on the objects. cc @floitschG. |
Changed the title to: "Document Expando to mention weak references". |
Expandos are not equivalent to weak references (meaning the Java WeakReference behavior). A weak reference is one that doesn't keep the referenced object alive, so the weak reference value may change to null at any time in the program. This makes GC visible in the program. Expandos are maps (from key to value) which won't keep the key alive. There is no way to distinguish an Expando that garbage collects the entry when the key dies, and one that doesn't, because you don't have the key to do the lookup any more. With expandos, GC need not be part of the language specification, it's just an optimization that implementations (are expected to) do to release memory that isn't needed any more. Disabling GC will not change the behavior of programs unless they run out of memory. |
@lrhn Hey, hope you don't mind, I used your explanation, word by word, here: https://pub.dev/packages/weak_map |
No problem. |
I think the documentation needs a clarification for object being "inaccessible". One case is that, if the property holds an reference to the object (i.e. cyclic reference within For Dart2JS this is a non-question. However since DartVM uses an array of |
We really prefer to not specify too much about expandos. You should treat them just as you treat instance fields, and not try to think about their behavior around garbage collection. The way things are currently implemented by the backends, you shouldn't worry about a value keeping its key alive. If you don't have a way to get access to the key without already having the key, then it is inaccessible, and will be garbage collected eventually. |
This issue was originally filed by @arhad
I consider that weak references may be introduced in Dart in near future when WeakMap will be supported in all popular browsers.
The general idea is that weak references may be transformed by dart2js to global WeakMap.
The text was updated successfully, but these errors were encountered: