VueComponent instances retained after calling vm.$destroy
See original GitHub issueVersion
2.6.10
Minimal reproduction
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<script src="https://unpkg.com/vue"></script>
<div id="app">
<base-link>Link</base-link>
</div>
<button id="action">Load</button>
<script>
let vm
const button = document.getElementById('action')
button.addEventListener('click', () => {
if (vm) {
vm.$destroy()
vm = null
button.textContent = 'Load'
} else {
vm = new Vue({
el: '#app',
components: {
BaseLink: {
template: `<a href="#" @click="onClick"><slot /></a>`,
methods: {
onClick(evt) {
evt.preventDefault()
console.log('clicked')
},
},
},
},
})
button.textContent = 'Destroy'
}
})
</script>
</body>
</html>
Steps to reproduce
- Load the app by clicking the button and take a heap snapshot
- Load/unload the app multiple times by clicking the button
- End in loaded stat and take a heap snapshot
- Search
Vue
in the memory snapshot viewer
I’ve personally tested this in Chrome.
What is expected?
Vue retained size should not increase
What is actually happening?
Vue retained size increases
Background: I’m using Vue and Turbolinks together in a project where the backend serves HTML with Vue templates inlined. This means I have to create and destroy Vue instances when navigating between pages.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
vue.js - Programmatically destroy a component from other ...
Now I want to destroy this posts.vue component when the user logs out.So that posts.vue component re-renders when the user logs in again....
Read more >Destroying a Vue instance - Coding Explained -
Learn how to destroy a Vue instance. Usually you will not have to destroy Vue instances manually, but in some situations it may...
Read more >API - Vue.js
After the instance is created, the original data object can be accessed as vm.$data . The Vue instance also proxies all the properties...
Read more >Tips for Unit Testing Vue Components with Jest - Medium
Common practice is to invoke shallowMount() and store mocked component in a wrapper before every test and destroy wrapper after each test.
Read more >Watch for Vuex State changes! - DEV Community
How to handle it in a Vue component? He asked a way to do it with Vuex (he was ... Next step is...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for the comment @kinow! In your example, you remove the dangling element, which seems to ensure that the VueComponent instance gets garbage collected. Seems like that after calling
vm.$destroy()
the link element is still bound to theonClick
method, thus leading into that the instance is retained in memory.Possibly related: https://github.com/vuejs/vue/issues/7086
I noticed the same issue on our project. Issue reproduced with version 2.6.10, 2.6.9 and 2.6.0.