Do I need to delete fragments at home or does the garbage collector remove them?

This might be a bit of a dumb question. I assume the garbage collector uses any hanging variables after the function completes, but I was wondering if this also applies to DOM fragments.

If I create a DOM fragment or any untethered node, if the garbage collector deletes it after the function finishes execution?

//would this create a memory leak? setInterval(function exampleScope() { var unusedDiv = document.createElement('div'); }, 10); 

I know this example is useless, but this is the simplest form of the template I'm worried about. I just want to know that I'm doing the right thing. I worked on creating a very high-performance JavaScript game engine, Red Locomotive . I do not want to add memory leaks.

+4
source share
2 answers

Well, it's not 100% convincing, but I did a quick JSFiddle to experiment with this. This is a hard loop that runs your code above 100000000 times. The memory usage of Chrome Task Manager increased from 47.9 MB to 130 MB and remained fairly constant until completion, where it dropped to 60 MB.

http://jsfiddle.net/u7yPM/

This suggests that the DOM nodes are definitely collecting garbage, otherwise memory usage will continue to increase for the entire test.

EDIT: I ran this on Chrome 14.

+3
source

There is an IE 7 memory leak with DOM elements in event handlers: jQuery leaks resolved, but why?

With another browser, everything will be fine. See Freeing up memory used by untied DOM nodes in Javascript .

If you are worried about memory leaks and caring for your illiterate IE users, you should read the following: Understanding and fixing Internet Explorer leak patterns

+4
source

All Articles