fork: fix memory leak in prefers-reduced-motion listener and one othe… #3304
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Potential Memory Leaks with Framer Motion + React Portal (Dialog)
We've observed memory leaks when using Framer Motion inside React Portals — specifically with modal dialogs.
On the first few renders, detached DOM nodes from the dialog content appear in memory snapshots. These leaks are more concerning when DOM nodes have event listeners that close over large data objects (e.g., closures referencing refs or component state).
Investigation
Using memory profiling tools, I traced two potential sources of retained memory:
window.matchMedia listeners – particularly for the "prefers-reduced-motion" query
The render queue in render-step
These seem to hold references beyond their intended lifecycle, contributing to retention of DOM nodes and React refs (e.g., VisualElement instances).
Note
These fixes may not be optimal or complete — the goal of this PR is to start a discussion and validate the approach with the Framer team. If more appropriate internal handling is possible, I’m happy to revise accordingly.
Related Issue
See discussion and example in: motiondivision/motion#2444