-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
Memory leak when using functional components in Vue 2.7.14 #13212
Comments
Vue 2 is no longer being maintained. |
Analysis of the Memory Leak Root Cause In this specific case, enabling functional: true might result in some internal Vue references (or developer-added references) to DOM nodes, causing the memory leak. Switching functional: false creates a Vue instance, which might handle these references more effectively and ensure proper cleanup. Steps to Mitigate
Example cleanup strategies:
Inspecting References: Use the Chrome DevTools Memory tab and inspect retained references to detached nodes to identify which closures or objects are retaining them. Using WeakMap or WeakRef: Replace long-lived references with WeakMap or WeakRef to prevent strong references to DOM elements. Reproduce in Isolation: Simplify the FunctionalComponent.vue to isolate the exact code causing the memory leak. For example:
Long-Term Solution Reporting the issue to Vue's GitHub repository if not already documented. |
@amirafa Thank you for the extensive answer. It makes sense that the Vue instance which is created with functional: false handles references properly while functional: true doesn't. In our project we opted for your first suggestion and removed functional components entirely. Interestingly, we were also able to mitigate the issue by rewriting the respective functional component in a js/ts file. However, we didn't analyse this further. |
@diegoexplicatis, @amirafa's answer is a copy-paste from chatGPT (or other LLM). |
We try to help each other not showing how cool we are but you're kinda right because it might be get wrong by some people |
Vue version
2.7.14
Link to minimal reproduction
https://github.com/diegoexplicatis/vue-2-functional-component-memory-leak
Steps to reproduce
See README.md of reproduction link
Project setup
Run project
How to reproduct memory leak
Open base url of project (printed in console)
Click on "Go to Test Component"
Click on Home
Open developer tools and take a heap snapshot
Filter for "Objects retained by detached DOM nodes"
See there are detached elements, e.g. a paragraph element with id = testParagraph.
Manually trigger garbage collection
Take another heap snapshot and filter for "Object retained by detached DOM nodes" again
See that e.g. the paragrpah is still being retained => paragraph element has leaked (it is detached from the DOM but is not being garbage collected)
Go to FunctionalComponent.vue to line 4 and change the parameter for functional to "false"
Redo steps 1 - 8
See that there are not detached elements (even without manually garbage collecting) => all elements have been properly garbage collected
Repeatedly executing steps 2. and 3. leads to an increasing memory usage i.e. repeating the steps five times leads to five detached paragraph elements that do not get garbage collected!
What is expected?
I would expect that after executing steps 1-8, e.g. the paragraph element is not being retained anymore and has been garbage collected independent of the value for the functional parameter in FunctionComponent.vue
What is actually happening?
Instead the paragraph element is being retained when using
functional: true
and does not get garbage collected.System Info
Any additional comments?
No response
The text was updated successfully, but these errors were encountered: