-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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: onclick handler holds onto detached DOM nodes #9346
Comments
try #5256 (comment) |
Adding Previously, ~960kb was allocated per mount/unmount cycle. With When the code is modified to manually mount/unmount onclick listener (without using Manual mounting/unmounting onclick listener code example + memory screenshot
Code example: <!-- Part of `src/components/TheListItem.vue` -->
<script setup lang="ts">
const onClickButton = () => { alert("clicked button"); };
const theButton: Ref<HTMLButtonElement | null> = ref(null);
onMounted(() => {
if (theButton.value) {
theButton.value.onclick = onClickButton;
}
});
onBeforeUnmount(() => {
if (theButton.value) {
theButton.value.onclick = null;
}
});
</script>
<template>
<div>
<button ref="theButton">Button</button>
</div>
</template> Retained memory in Allocation instrumentation on timeline in Chrome: |
This is a Chrome bug. see https://bugs.chromium.org/p/chromium/issues/detail?id=1213045 <img :src="src" :title="alt" loading="lazy" />
+<img :src="src" :title="alt"/> |
That explains 137kb leaking when mounting/unmounting a component My primary question, however, is mostly about memory not being freed when using When manually adding onclick handler in But when using <script setup lang="ts">
const onClickButton = () => { };
</script>
<template>
<div>
<button @click="onClickButton">Button</button>
</div>
</template> |
I checked it again, it seems to be caused by I guess this is a duplicate of #5363 then Thank you 🙂 |
Vue version
3.3.4
Link to minimal reproduction
https://github.com/nWacky/nuxt-15239/tree/vue-only-reproduction
Steps to reproduce
In App.vue there is a toggle button and TheList component
TheList
component renders a list of child components TheListItemWhen clicking the toggle button, about 900kb of ram is allocated to show
TheList
.When clicking the button again,
TheList
is unmounted.900kb is not cleared up after forcing gc in Memory inspector in Chrome.
Clicking the toggle button again allocates another 900kb for
TheList
.What is expected?
When
TheList
is unmounted, the ram should be clearedI think this is a memory leak
What is actually happening?
Memory inspector in Chrome points to context somewhere in createInvoker().
Removing @click="onClickButton" from
TheListItem
reduces leaked memory from 988Kb to 160Kb per mount/unmount cycle.Perhaps, onclick listener is never unmounted, and is holding onto detached DOM nodes
System Info
Any additional comments?
Possibly related issue: nuxt/nuxt#15239
The text was updated successfully, but these errors were encountered: