views:

214

answers:

1

Hi there,

I was recently looking at this memory leak tool sIEve: http://home.orange.nl/jsrosman/

So I decided to test out the tool by creating a main page that will open up a popup window. I started by creating 3 pages: index.html, page1.html and page2.html, the index.html page will open a child window (popup) linking to page1.html. Page1 will have a anchor tag that links to page2.html, while page2 will have a link back to page1.html

PROBLEM

So in the tool I entered the index.html page, popup window opened to page1.html, I then clicked the page2 link, no leaks detected yet.

While I'm on page2 I click the link back to page1, and that's where the tool claims there is a link. The leak seems to be happening on the index.html page and I have no idea as to why it would be doing that. Even more concerning is that I can see elements that the tool detects that aren't even on my page.

Does anyone have any experience with this tool or know if this really is a memory leak? Any samples of showing how to achieve what I'm doing without memory leaks?

INDEX.HTML

  <script type="text/javascript">

   MYLEAK = function() { 
    var childWindow = null;

    function showWindow() {
     childWindow = window.open("page1.html", "myWindow");                 
     return false;
    }         
    return {     
     init: function() {
      $("#window-link").bind("click", showWindow);               
     }      
    }       
   }();   

  </script>

 </head>

<body> 
 <a id="window-link" href="#" on>Open Window</a>

 <script type="text/javascript">   
  $(document).ready(function() {
   MYLEAK.init();   
  });
 </script>

</body>
</html>

PAGE1.HTML

<html>
<body>

 <h1>Page 1</h1>
 <a href="page2.html">Page2</a> 
</body>
</html>

PAGE2.HTML

<html>
<body>
 <h1>Page 2</h1>
 <a href="page1.html">Page1</a> 
</body>
</html>

Appreciate your efforts.

+1  A: 

I have test this leak on Windows 2000 5.00.2195 service pack 4 with following specs of IE

version: 6.0.2800.1106IS
Chiper Strength : 128-bit
Product ID 55736-837-5565635-04173
Updated Versions:;SP1;Q823353;Q833989

Keep a reference to the newly opened window, and when you close it (or reopen it), set the reference to null. That way the garbage collector will collect (eventually) the window object. If you don't, the window object stays in the memory, and noone know when it will be deleted.

Additionally, you should always name your windows (but that has nothing to do with your problem).

Something like:

var wPopUp = null;
function clickButton()
{
if ( wPopUp )
{
if ( !wPopUp.closed )
{
wPopUp.close();
}
wPopUp = null;
}

wPopUp = window.open('leak2.html',
'wPopUp', 'width=500, height=400');
}

also have a look at MS JS Mem Leak detector

Since IE is unable to do its job and reclaim the cycles, it falls on us to do it. If we explicitly break the cycles, then IE will be able to reclaim the memory. According to Microsoft, closures are the cause of memory leaks. This is of course deeply wrong, but it leads to Microsoft giving very bad advice to programmers on how to cope with Microsoft's bugs. It turns out that it is easy to break the cycles on the DOM side. It is virtually impossible to break them on the JScript side.

When we are done with an element, we must null out all of its event handlers to break the cycles. All we have to do is assign null to each event handler's property. This can be done very specifically, or we can make a generic purge function.

XGreen
Hi there,Thanks for the reply, I think I have tried that before in another test I put in my showWindow() method after I opened the window childWindow = null is that what you meant, or do you mean keep a reference to it on the parent window? also when you say name my window didn't I do that with: childWindow = window.open("page1.html", "myWindow"); ?
DotnetShadow
I updated my answer Shadow
XGreen
Thanks for that I tried your code by just changing my binding to:$("#window-link").bind("click", clickButton);Initially there are no leaks when you open the window, but if you click the links on the popup window Page2->Page1->Page2 then leaks occur. I have no problem if I just open the popup up straight to Page1, it's only when I click the link on that page that points to page 2 that the leak occurs not sure if it that's what you saw?At this stage I'm inclined to think that the tool isn't actually correct
DotnetShadow