JavaScript garbage collection and memory leaks

Source: Internet
Author: User

JavaScript uses the garbage auto-recycle mechanism, which automatically cleans up memory that is no longer in use, so JavaScript does not have to manually free up useless memory as in languages like C + +.

Before that, let's talk about garbage collection in two ways: reference count and tag cleanup.

The reference count method sets the counter for each allocated memory unit, and when the counter is reduced to 0, it means that the unit can no longer be referenced and will be purged.

One problem is that when there is a circular reference, the memory cell's counter will never be 0, and the memory release will be more complex (need to use a weak reference).

1 obj.val = obj2; 2 obj2.val = obj;


The Mark Purge method maintains a linked list, which is added to the list when the variable enters scope, and is removed from the list when it is moved out of scope. When the GC is activated, a tag is first marked for each variable, and then the tags of the variables that exist in that list are cleared, as well as the tags of the members referenced by the variables. Finally, variables that are no longer used are still flagged by the GC and will be freed, including circular references.

If a piece of memory that is no longer in use is not reclaimed, it will lead to memory leaks, which will always occupy memory and cannot be exploited, which can cause problems such as slow system operation, browser crashes, and so on.

There are divergent opinions about what kind of recycling mechanism is used by the browser's JavaScript implementations.

I Google a bit, http://www.ibm.com/developerworks/web/library/wa-memleak/? S_tact=105agx52&s_cmp=cn-a-wa mentions that IE and Firefox use reference counting mechanisms to reclaim DOM objects, http://blogs.msdn.com/b/ericlippert/archive/2003/09 /17/53038.aspx says JScript uses nongenerational mark-and-sweep garbage collector (a kind of markup cleanup), and data mentions that modern browsers use tag cleanup to reclaim JavaScript garbage.

To summarize, the browser reclaims JavaScript memory with tag cleanup and uses reference counting to reclaim host objects such as DOM, Bom, ActiveX object.

Based on my tests on IE, circular references between JavaScript objects do not cause a memory leak.

SetInterval (function() {    for (var i = 0; i < 100000; i++) {           var obj = {}, Obj2 = {};         = obj2;         = obj;     10);

Memory usage is cyclical and stable, so there's no need to worry about circular reference problems with JavaScript objects.

Now that the DOM uses reference counting to reclaim memory, is there a memory leak problem?

var nodehold = [],    = setinterval (function() {        for (var  i = 0, length = 1000; i < length; i++) {            var node = document.createelement ("div"),            = {};             = obj;             = node;            Document.body.appendChild (node);            Document.body.removeChild (node);         500);

On IE7 and IE8, the memory line goes up.

Consistent with http://blogs.msdn.com/b/ericlippert/archive/2003/09/17/53038.aspx, because JavaScript garbage collection does not control DOM objects, and Dom uses reference count recycling, which causes circular references to not be recycled. The premise is that the DOM has to be added to the document tree and then deleted (I guess it's allocating memory for a real DOM object, which is not JavaScript).

It is important to note that ie9+ does not have a circular reference causing a DOM memory leak, either by Microsoft optimizations or by the way the DOM is recycled.

JavaScript garbage collection and memory leaks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.