UPDATE: I posted this question a year ago, funnily enough I have the same issue on a project I'm working in mainly IE7. Basically I got IE7 in WinXP SP3 virtual pc installed sIEve and memory leaks would occur with the code below. Basically I want no code leaks with the sample code I provided below, any help would be appreciated
I was recently looking at this memory leak tool sIEve: http://hom开发者_StackOverflowe.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.
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.
精彩评论