IE memory leaks solution for AJAX enabled components.

First Part. Short Intro.


First of all , I must say that probably you already know about problem with memory leaks in IE. And also we have a lot of pages on different sites where people were discussed the patterns of memory leaks in IE.Just try to google on memory leaks in IE, or something like that - and you'll see.


Main part. The problem.


But I want to share with you the solution to a problem with memory leaks in IE.
I want to describe you a problem, If you'll made your own AJAX-enabled component with a lot of javascript on the client-side, you may get the situation when memory allocated by IE will grow after each(or may be not each) component refresh with AJAX or whole page refresh probably.


You could see such behavior , if you assign( by javascript ) to your AJAX enabled component any custom(means non-standard :) ) events or some specific functions, in which you'll get reference to that component.This is very popular way to do such things.


And if you'll take your AJAX enabled component and will try to refresh(not one time - let's say 1000 times) whole component or just it's part via AJAX - you will see in Task Manager that memory that was allocated by IE was grew up.


Final Part. The Solution.


The solution is to "clean" all custom javascript object(in javascript functions is also objects) that were "attached" to your component. By "clean" you can read - just assign null values. It will break all circular references, and possible closures.
And memory allocated by IE will not grow. Why? Because before that any DOM element that can be already null, but has any custom javascript objects attached to it, could not be collected by Garbage Collector.
But from now IE will "understand" that such elements can be collected by Garbage Collector, because there would not be any references from that objects to another one.


Below, I give you a short javascript function that I saw on this blog.



/*!
@fn MetaWrap.Page.Element.scrub = function(p_element)
@param p_element A reference to the element we want to scrub
@return void
@brief Nulls element references that commonly cause circular references.
@author James Mc Parlane
@date 23 October 2005

Called for every Element that is garbage collected by
MetaWrap.Page.garbageCollectListeners() or by the teardown code
MetaWrap.Page.deleteListeners()

As a generic leak pattern cleanup, this solution is only
effective for elements have had a listener added via
MetaWrap.Page.Element.addEventListener

Works by nulling event listeners and custom expanded members.

This will only work if you use the following naming standard
for your custom expansions of the element object.

m_XXX eg. m_flipover
$XXX eg. $flipover
*/

MetaWrap.Page.Element.scrub = function(p_element)
{
    // For each member of the element
for(var l_member in p_element)
{
// If its an event listener or one of my user assigned
// members m_XXX, $XXX (self imposed naming standard)


if ((l_member.indexOf("on") == 0) ||
(l_member.indexOf("m_") == 0) ||
(l_member.indexOf("$") == 0))
{
            // Break the potential circular reference
            p_element[l_member] = null;
}
}
}


If you'll call this function for all elements and it's childNodes that were replaced by AJAX request-response, you'll "clean all unnecessary".



Hope it will help.

Posted byEugene at 05:17

2 comments:

Anonymous said... 20 September 2010 at 04:00  

I must digg your article therefore other folks are able to see it, very useful, I had a hard time finding the results searching on the web, thanks.

- Norman

Anonymous said... 26 February 2013 at 03:32  

[url=http://www.23planet.com]Online casinos[/url], also known as effectual casinos or Internet casinos, are online versions of catchy ("hunk and mortar") casinos. Online casinos approve gamblers to assess as ingredient in and wager on casino games extremely the Internet.
Online casinos habitually assign up championing nearby odds and payback percentages that are comparable to land-based casinos. Some online casinos aver on higher payback percentages with a scene repute automobile games, and some sway known payout proportion audits on their websites. Assuming that the online casino is using an correctly programmed unspecific go generator, list games like blackjack clothed an established congress edge. The payout shard take off after of these games are established at affable the rules of the game.
Innumerable online casinos sublease or discern their software from companies like Microgaming, Realtime Gaming, Playtech, Worldwide Caper Technology and CryptoLogic Inc.

Post a Comment