Questions about memory leak JS (try/catch, images in b64, ajax, unset variable, removeChild)
OK, i have a list with a few things that makes think that is the reason why my add-on is using a lot of memory.
1 - Use try/catch can increase memory usage? Example:
try{
if(!condition) throw "Message";
//some code
}catch(ex){}
If this is the problem,I was thinking in use:
(function(){
if(!condition) return;
})();
2 - Use images with base64 can improve memory usage too?
<img src="data:image/png;base64,[...]==" />
Or there is no problem?
3 - I use a function to handle ajax result, so i can use responseXML...
if (o.readyState != 4) return;
var newdoc = document.implementation.createDocument(nul开发者_开发问答l, null, null);
var newhtml = document.createElement('div');
newhtml.innerHTML = o.responseText.replace(/script/ig, "");
newdoc.appendChild(newhtml);
newdoc.getElementById('...');
I cannot use jquery, this is the easiest way that I found to use responseXML, but may causing memory leak too.
4 - Unset variables, helps in something or is unhelpful?
var a = "something";
if(a=="something")
//
//I'm not going to use the variable "a" anymore, so..
a = undefined; //Now i unset the variable
5 - If you add an Event to a element, or you use appenChild, this increases memory.
element.appendChild(newelement);
If you change the page, looks like the memory is still the same.. If I use
window.addEventListener("unload", function() {
element.removeChild(newelement)
}
This helps in something too?
Thanks.
Sorry for the english.
On 4 and 5th.
Browser realy increases memory on every additional event\element, but don't decreases it immedialtely after some events\elements got deleted.
In garbage collecting every browser has it's "Dao":) :
IE 6-8 don't clear memory at all (until you close it or press F5).
Chrome takes up to 5 minutes to run garbage collector or when garbage size reaches some fixed amount.
Opera "eats" more memory then any other browser, but frees it immediately after it's became unneeded.
The fastest is FF nearly 1 minute to clear memory from unused data.
精彩评论