The following is an article that I posted about one and a half years ago. I just repost it here as this is a hack to build up Java2Script Pacemaker.
—————————————————-
Yes, I knew there was JavaScript memory leak in IE. I developed JavaScript codes mainly by Firefox/Mozilla, so I won’t notice the codes were leaking memory. These days I was developing J2S application in Eclipse, and found that the J2S applications were runnig more and more slowly in the J2S Console, which embed IE Browser widget. Once I did not think that memory leaking will make such poor performance. The slowness that I couldn’t stand finally taught me the lesson and I decided to fix the leaking codes.
Searching the net, I found that lots of articles were talking about this problem. I mainly read Justin Rogers’ Understanding and Solving Internet Explorer Leak Patterns. And after finishing the articles, I monitored one J2S SWT application (SWT Tree Snippet) while tring to refresh the page. I found 5 times of refreshing will leaking the memory from about 22M to 31M! Then I knew that maybe about 100M+ of memories was leaked in developing J2S applications when I felt the slowness of J2S Console.
I added a handler to the “onunload” event to break those circular references, codes like following:
/*
* Only IE need to release the resources so that no memory is leaked
*/
if (window.attachEvent) {
window.attachEvent (“onunload”, function () {
try {
org.eclipse.swt.widgets.Display.releaseAllResources ();
} catch (e) {
}
return true;
});
}
In the static method org.eclipse.swt.widgets.Display.releaseAllResources, I set those elements’ on* handlers to null and set those Controls’ parent and children to null. Things did work much better. But it was not fixed compeletely. Once I refreshed for a couple of times, I noticed that the browser will leak some 10~100k gradually. That was to say it will take about 10~100 times of refreshing for about 1M leaking and users will only perceive the slowness after about 1000 times of refreshing. So it was considered as working better.
I tried other ways to solve this leaking but did not get the right entrance.The current Java2Script library is almost built on closures of JavaScript, which will leak memory easily and hide leadking codes deep inside the whole codes. Replacing all those codes that using closures? No. Without closures, it’s somewhat hard to implement the Java Class inheritance.
It’s as Justin Roger’s saying “not all memory leaks are easy to find”. Memory leaking is just awful enough! 🙁