garbageCollect static method

void garbageCollect()

Removes any weak references to disposed AutoDisposableValue instances from the internal weak reference set and disposes of their associated values in the internal value map.

Since AutoDisposableValue instances are automatically disposed of when they are no longer needed, you may end up with many instances that have been disposed of but whose weak references are still taking up memory. By calling garbageCollect, you can remove these weak references and free up memory.

It's recommended to call garbageCollect periodically, such as during idle periods in your application, to keep memory usage under control.

Implementation

static void garbageCollect() {
  _weakRefs.removeWhere((ref) {
    final instance = ref.target;
    if (instance == null) {
      return true;
    }
    if (instance.isDisposed) {
      _values.remove(instance._key);
      return true;
    }
    return false;
  });
}