weak_map 1.0.5 copy "weak_map: ^1.0.5" to clipboard
weak_map: ^1.0.5 copied to clipboard

outdated

WeakMap is a map of key/value pairs in which the keys are weakly referenced. WeakContainer is a weak-reference that only lets you check if some object it the same you had before.

weak_map #

This package contains the classes:

  • WeakMap
  • WeakContainer

Why is this package useful? #

Dart doesn't allow for real weak-references, but this package allows you to go as close as possible (internally it uses the Expando class).

The Dart engine stores a value in memory while it is reachable (and can potentially be used). Usually, keys in a map are considered reachable and kept in memory while the map itself is in memory. This means if we put an object into a map or into a variable, then while the map is alive, the object will be alive as well, even if there are no other references to it. It occupies memory and may not be garbage collected. WeakMap and WeakContainer are fundamentally different in this aspect. They don't prevent garbage-collection of key objects.


WeakMap #

A WeakMap lets you garbage-collect its keys. Please note the keys can be garbage-collected, not their corresponding values.

This means if you use some object as a key to a map-entry, this alone will not prevent Dart to garbage-collect this object.

In other words, after all other references to that object have been destroyed, its entry (key and value) may be removed automatically from the map at any moment. To create a map:

var map = WeakMap();

To add and retrieve a value:

map["John"] = 42;
var age = map["John"];

The following map methods work as expected:

map.remove("John")
map.clear()
map.contains("John"))

However, adding some null value to the map is the same as removing the key:

map["John"] = null; // Same as map.remove("John")

Notes:

  1. If you add a number, a boolean, a String, or a const type to the map, it will act like a regular map, because these types are never garbage-collected. All other types of object may be garbage-collected.

  2. To retrieve a value added to the map, you can use the equivalent syntaxes var y = map[x] or var y = map.get(x).

  3. Doing map[x] = y is equivalent to map.add(key: obj, value: 42), and will mean the object is later retrieved by equality. However, you can also add the object so that it is later returned by identity. To that end, use the syntax map.addByIdentity(key: obj, value: 42).


WeakContainer #

As previously explained, Dart doesn't have real weak-references. But you can check that some object is the same you had before.

To create a weak-container:

var obj = Object();
var ref = WeakContainer(obj);
var someObj = Random().nextBool() ? obj : Object();
print(ref.contains(someObj)); // True or false.

This will print true if someObj is the same as the original obj, and will print false if it's a different object, compared by identity. If all references to the original obj have been destroyed, the weak-container will not prevent obj to be garbage-collected.


Why doesn't Dart allow for real weak-references, anyway? #

Because the creators of Dart don't want the GC (garbage-collector) to be "visible".

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 the 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 anymore.

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, the 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 anymore. Disabling the GC will not change the behavior of programs unless they run out of memory.


The Flutter packages I've authored:

My Medium Articles:

My article in the official Flutter documentation:


Marcelo Glasberg:
https://github.com/marcglasberg
https://twitter.com/glasbergmarcelo
https://stackoverflow.com/users/3411681/marcg
https://medium.com/@marcglasberg

16
likes
0
pub points
87%
popularity

Publisher

verified publisherglasberg.dev

WeakMap is a map of key/value pairs in which the keys are weakly referenced. WeakContainer is a weak-reference that only lets you check if some object it the same you had before.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

meta

More

Packages that depend on weak_map