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
Detached nodes after unmount component(Memory leaks) #5363
Comments
This comment was marked as outdated.
This comment was marked as outdated.
@Akryum Do you agree with @caozhong1996? Should we move this issue to the devtools repo? |
@caozhong1996 memory leak reproducible, I showed an example without vue-devtools(prod build npm run build) see url in second screenshot ends with dist/index.html , and i reproduce in incognito mode, please check again |
Sorry, I misunderstood you, it's similar to vue-devtools's behavior, but it's another problem😥. It seems that those invokers not be cleaned during unmount.
|
@caozhong1996 Thanks for the research, and what to do about it? |
@caozhong1996 Interesting. I don't see an apparent reason though why these should not be garbage collectable - the elements are detached, the reference to the component instance in the invoker's closure should also not be a reason as those instances have been unmounted and detached as well. Do you have an idea/suspicion? |
I'm just as confused by this as you, even in this situation which still appeared😥 <!-- no event binding -->
<div v-for="item in 1000" v-bind:key="item">
<div>111<div/>
</div> |
@caozhong1996 what exactly confused you ? |
We just have a hard time figuring out a reason as to why detached elements aren't garbage collected. |
@LinusBorg In view 2 the same behavior of detached nodes after destoy App. vuejs/vue#12445, maybe this isue for vue 2 will be useful |
@caozhong1996 Event binding is in the example, on the button element |
@LinusBorg Now I'm pretty sure it's a bug. We created invoker at here and reference component instance. core/packages/runtime-dom/src/modules/events.ts Lines 107 to 132 in c35ec47
These elements reference invoker's closure (_vei) core/packages/runtime-dom/src/modules/events.ts Lines 81 to 82 in c35ec47
So after removeChild, these elements become detached, even can't remove component instance. core/packages/runtime-dom/src/nodeOps.ts Line 17 in 9aa5dfd
Because we should remove these references before removeChild:
|
the invoker closure doesn't reference the element, it only references the instance. the MDN statement you quoted refers to things that are still active on the main thread (Whatever the correct term may be) having a reference on the removed element, which is not the case here? |
@LinusBorg here we go |
Any update on this? This memory leak is hurting our application's performance. |
I investigated the repro and noticed a few very complicated conditions. For the original reproduction, the "leak" only happens if I click the button first, then unmount the app via the inspector + console.
If you change const app = createApp(App)
app.mount('#app')
setTimeout(() => {
app.unmount()
}, 1000) The leak doesn't happen after app is unmounted. If you add a button outside of the app (in The leak does happen if the unmount is triggered by an event dispatched from an element within the unmounted parent element. Interestingly, this seems to be a Chrome bug - because I was able to reproduce this without Vue involved at all in this gist. In addition, this leak seems to be "temporary" - i.e. if this process is repeated by clicking the "reset" button in the above gist, the total amount of nodes in memory does not increase. In fact, I've noticed that the detached nodes are garbage collected when another event listener is triggered. Previously detached nodes are somehow reclaimed. So in some way, this isn't really a "leak" since the total memory does not increase over time. To sum up: this seems to be a Chrome issue, and I couldn't find any way to "fix" this in Vue, since even replacing current event patching with plain |
Try @JhonWeawer 's reproduce repo on chrome 104.0.5112.79 with vuejs@3.2.37, still reproduce this problem. It seems not a chrome issue or the fixed issue still not work with vuejs. @LinusBorg @yyx990803 Is this would be fixed? It hurts performance. |
I am seeing the same issue of a large number of detached html elements. However, I have noticed this in chrome. Firefox works fine. If this is a chrome issue, can someone link it here? |
I tested @yyx990803's gist in chrome 104 and 112, while the chromium issue appeared to have been fixed in 104, it's back in 112 or something similar anyways. I also tested the original reproduction with Vue 3.2.47 in both chrome 104 and 112 and I see detached nodes in both versions, so as mentioned above it doesn't seem like it was ever fixed. |
Version
3.2.29
Reproduction link
github.com
Steps to reproduce
first step run npm run build, open dist/index.html
highlight element with id app , write in console
$0.__vue_app__.unmount()
after unmount app create heap snapshot, we have memory leaks in heap snapshot
What is expected?
zero detached dom elements and detached vue EventListeners...
What is actually happening?
exist in heap snapshot:
2001 Detached HTMLDivElement
Detached EventListener
...
The text was updated successfully, but these errors were encountered: