Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Vue Components Not Being Destroyed

If I load my app and take a Chrome memory heap snapshot straight away, I get the following results.

enter image description here

If I click around my web app and then return to the original page loaded, and take another memory heap snapshot I get the following results.

enter image description here

From this, we can see there are now ~10x the number of VueComponents and an increase in the number of Vue instances.

This is having a big impact on memory usage of the app.

What tools/methods are available to track down the culprit components that are not being destroyed?

like image 970
Josh Avatar asked Apr 03 '19 14:04

Josh


People also ask

Does V Show destroy the component?

v-if is “real” conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.

What is SFC in Vue?

Vue Single-File Components (a.k.a. *.vue files, abbreviated as SFC) is a special file format that allows us to encapsulate the template, logic, and styling of a Vue component in a single file.

How do I force refresh Vue?

The best way to force Vue to re-render a component is to set a :key on the component. When you need the component to be re-rendered, you just change the value of the key and Vue will re-render the component.

How do you hide and show components in Vue?

Vue gives you a bunch of good ways to hide the element on the screen. When using v-if="false" the element isn't rendered at all in the DOM. When using v-show="false" the element is rendered in the DOM, however, Vue applies the inline style display: none that hides the element completely.


1 Answers

Tracking Down Orphaned Vue Components

As the question suggested, I was pretty sure these memory issues were caused by orphaned Vue components that were not being cleaned. It is possible to look through the source for circular references etc... and fix manually.

I was looking for a way to find the specific Vue components so I could narrow my search in the code.

Using Heap Snapshot

The best method I have found does require some manual effort but works pretty well.

From the Chrome dev tools Heap snapshot we can search for detached

N.B. You must compile Vue for dev, not production, or this will not work

how to search for detached nodes

This will show a list of elements that have been detached from the DOM.

We can now click on one of these items to highlight it.

selecting a detached element

Then we can head over to the Chrome dev tools console and type $0 to get the element.

showing selected element in console

From this I can see that the email input from my login page is the culprit and so I should investigate why this is not being destroyed.

Conclusion

While this does provide a good way to track down components that are not being destroyed, please note that not all detached nodes are bad, apply the context of your app to decide whether it is a memory leak or not.

like image 127
Josh Avatar answered Sep 29 '22 16:09

Josh