1.

Which lifecycle hook plays a great role for memory leak not to occur?

Answer»

A memory leak occurs when memory is allocated for use and not deallocated where the memory area is no longer needed. It is caused by a PROGRAM that does not deallocate (free up) the extra memory allocated and FREQUENT memory leaks might halt everything. 

  • In VUE.js, memory leak does not usually come from Vue itself, rather other third-party libraries CAUSE a memory leak by manipulating the DOM or by creating their instances. 
  • For memory leaks not to occur, we have to manually clean up any third-party instances in the beforeDestroy() lifecycle hook. 

Example:  

mounted() {    this.chart = new PowerGraph();  } 

When the component is mounted the third-party library is created so it has to be destroyed in the beforeDestroy() lifecycle hook for memory leak not to occur as shown below. 

beforeDestroy() {    this.chart.destroy();  } 

If the clean-up is not done before our component gets destroyed, then that memory is never going to be RELEASED. Hence, a memory leak occurs. 



Discussion

No Comment Found

Related InterviewSolutions