Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Allow observers to cleanup if their root dependency has no references

Open dphfox opened this issue 3 years ago • 0 comments

Currently, Observer is only eligible for garbage collection when it has no active change handlers.

The original motivation was that, because Observer allows for side effects, it may influence code outside of the original scope in which it existed. This made it unsuitable for Fusion's standard 'weak-dependency' garbage collection model, which relies on explicitly disallowing side effects. As a quick fix, the current approach was employed to make sure changes weren't being missed, while still allowing for the memory to be freed after use to avoid memory leaks.

However, there is still a theoretically better approach we should investigate at some point. If the root dependency of the observer has no (non-read-only) references held, then the reactive graph will never experience another update, and so it's safe to allow for garbage collection. This works because all state objects (Value, Spring, Tween) that generate state - as opposed to transforming state - are only held in memory while they may change, and so they will only hold references to their dependencies while they are changeable.

dphfox avatar Feb 08 '22 20:02 dphfox