Example Analysis of memory leak in vue
This article mainly introduces the example analysis of memory leak in vue, which is very detailed and has certain reference value. Friends who are interested must finish it!
What is a memory leak? A memory leak means that a piece of memory is new but cannot be freed or garbage collected. After new has an object, it requests to occupy a piece of heap memory, and when the object pointer is set to null or goes out of scope and is destroyed, the memory will be automatically garbage collected in JS when no one references it. But if the object pointer is not set to null, and the code surface can no longer get the object pointer, it will not be able to free the memory it points to, that is, a memory leak has occurred. Memory leakage (Memory Leak) means that the dynamically allocated heap memory in the program is not released or can not be released for some reason, resulting in a waste of system memory, slowing down the running speed of the program, or even system crash and other serious consequences. 1. The echarts diagram is not completely deleted; 2, setTimeout and setInterval are not cleared; 3, the global setting variable is not cleared; 4, the listener is not cleared
Scene analysis
Where the global object onresize, listening events should be cleared before the group price is terminated.
Important: in vue, echarts drawing is very resource-consuming, so be sure to clear the corresponding data before the component is destroyed.
The definition in data is as follows:
Before destroying a component, you should do the following:
1 in vue
2 in vue
3keep-alive
Once you use keep-alive, you can access two other lifecycle hooks: activated and deactivated. If you want to clean up or change the data when a keep-alive component is removed, you can use the deactivated hook.
Deactivated: function () {/ / remove any data you don't want to keep, or destroy places that may cause memory leaks} these are all the contents of the article "sample Analysis of memory leaks in vue". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!