I am loading entire data of .html files in a div via ajax call. The html page contains link,script tags required for HTC(html component) to bind with the elements on page.
The structure of the page is like:
I am using jQuery to load HTML pages like this
<body>
<input type="radio" class="openpage" id="0001">
<input type="radio" class="openpage" id="0002">
<input type="radio" class="openpage" id="0003">
<input type="radio" class="openpage" id="0004">
<div id="loadPage"></div>
<script>
$(document).ready(function(){
$(".openpage").on("click",function(){
$("#loadPage").load($(this).attr("id")+".html",function(){
//load a page with many htc bindings and external references
//trigger some onload functions which are not called automatically in IE
//confirmed this is in chrome
});
});
})
</script>
</body>
The problem with this is that IE memory keeps pilling up as radio buttons are clicked to load pages and after loading few pages browsers stops responding. I can see the memory going up in task manager.
The application runs in IE 5,6,7,8,9 and in quirks mode in IE10. I tried to replicate the same scenario in chrome and it showed no problem. So I think it is IE specific memory management related problem.
I read many articles but I was not convinced.I also read this question on SO but it suggests to load only some part of page which is not acceptable in my case.This article seems relevant but I am not sure what exactly causes memory leak in my case.
The entire body above is actually in a iframe . So as a workaround I reload the frame with the main page containing after 8 clicks of radio button. I have tried to clear stuffs before each load using:
$("#loadPages").empty();
$("#loadPages").find("*").off();
$.ajaxSetup({ cache: false });
But nothing works.So I doubt whether jquery can clear events registered through htc.
1) It would be great if you explain the reason of memory leak in this case.
2) And in general why memory leak can occur in a browser after loading many resources. This does not happen in modern browsers but why it occurred in older browsers.
3) Is there a way to release browsers memory without refreshing the frame.
4)Is there a way to check what all resources are residing in a browsers memory.
You have a couple of workflows:
From here:
Your options are:
IE has separate COM channels for the JavaScript and the Rendering engine, this means that sometimes, particularly with event binding. Objects/state in JS can hold on to references to UI/DOM nodes... and DOM nodes can reference JS event handlers. In many cases this means that those portions won't be garbage collected leading to inflated memory usage. It was far worse with earlier versions, but still happens.
You can have similar issues with other browsers, but IE tends to display this behavior much sooner. This also happens more with long-living single page applications.
If you only load each resource once, into its' own DOM node, you can simply detach/reattach that node. Also, you can rely on event propagation to listen to events from the parent node. Either of these will reduce the risk of contention, and overhead.
As to unregistering your event handlers, jQuery does a better job than most with this, as long as you register with jQuery, and use jQuery to remove/delete the node (and its' children).