Saturday, December 29, 2012

Best practices to reduce memory leak in jQuery/Javascript



1. if you want to remove HTML DOM elements,  follow the below  steps to release memory claimed by DOM elements.

a) $("#content").off();   // unbind all the events of that control.
b) $("#content").empty(); or   $("#content").html("");  // -- Remove the child elements before removing the element itself.
c) $("#content").remove();  // -- remove the element itself.

2. Before removing the element, please follow the above three steps for each child element from lower level to higher level.

3. if you have used any variable to store the values, follow below steps to release memory claimed by variable.

delete x;  // x is variable
x = null;

4. if your plugin has functions or properties, please set it to null.

[object].sortable = null;

No comments: