Skip to content
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

Open
DartBot opened this issue Jan 17, 2014 · 11 comments
Open

Document Expando to mention weak references #16172

DartBot opened this issue Jan 17, 2014 · 11 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-documentation A request to add or improve documentation

Comments

@DartBot
Copy link

DartBot commented Jan 17, 2014

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.

@lrhn
Copy link
Member

lrhn commented Jan 17, 2014

Removed Type-Defect label.
Added Type-Enhancement, Area-Library, Triaged labels.

@lrhn
Copy link
Member

lrhn commented Jan 20, 2014

Could you use Expando for the things you want weak references for?

@DartBot
Copy link
Author

DartBot commented Jan 21, 2014

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.

@DartBot
Copy link
Author

DartBot commented Jan 22, 2014

This comment was originally written by @arhad


Reassign this enchancement to documentation area, please.

@lrhn
Copy link
Member

lrhn commented Jan 23, 2014

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.
Removed Type-Enhancement label.
Added Type-Documentation, Library-Core labels.

@sethladd
Copy link
Contributor

Changed the title to: "Document Expando to mention weak references".

@lrhn
Copy link
Member

lrhn commented Feb 25, 2015

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.
Basically, it means that an expando keeps a value alive as long as you have a reference to both the expando and the key, and after that, you can't check if the entry is there or not.

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.

@DartBot DartBot added type-documentation A request to add or improve documentation Priority-Unassigned area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core labels Feb 25, 2015
@lrhn lrhn removed the core-a label Aug 11, 2017
@marcglasberg
Copy link

@lrhn Hey, hope you don't mind, I used your explanation, word by word, here: https://pub.dev/packages/weak_map

@lrhn
Copy link
Member

lrhn commented May 19, 2020

No problem.

@znjameswu
Copy link

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 Expando key-value pairs), does that interfere with the accessibility of the object?

For Dart2JS this is a non-question. However since DartVM uses an array of WeakProperty (whose details are hidden inside VM natives) as the underlying Expando implementation, one would naturally comes up with the above question.

@lrhn
Copy link
Member

lrhn commented Oct 12, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-core type-documentation A request to add or improve documentation
Projects
None yet
Development

No branches or pull requests

6 participants