JavaScript garbage Collection (ii)--garbage collection algorithm

Source: Internet
Author: User

A, reference counting (Reference counting) algorithm

The DOM and BOM under Internet Explorer 8 use COM components so it is the reference count that handles memory for DOM objects, and the reference count means that the number of times each value is referenced is tracked. Image Point says:

1) There is a lot of sticky paper in the house, which is like memory. Such as:

  

2) using memory is like writing on these papers. However, there is a condition that anyone using a piece of paper must write a count of 1 in the corner of the paper, and if 2 people use a piece of paper at the same time, the Count becomes 2, and so on. When a person is finished using a piece of paper, the count of the corners must be reduced by 1, so that once the count becomes 0, the garbage collection condition is satisfied, and the robot that waits aside immediately throws the piece of paper into the bin. However, there is a flaw in this algorithm, which is that once a circular reference is generated, the memory is compromised.

  

  

Second, Mark-Clear (mark-sweep) algorithm

When a variable enters the execution environment, such as declaring a variable in a function, the garbage collector marks it as "going into the environment" and marks it as "out of the environment" when the variable leaves the environment (function execution ends). Image Point says:

1) The leader sends some sticky notes to the staff, use them casually, do not need to make any mark.

  

2) At some point, the leader commands everyone to stop, and each person makes a mark on the white paper that they still need to use.

  

3) The final leader throws all the unmarked White paper into the dustbin.

  

4) The defect of this algorithm is also obvious, the first is the efficiency problem, in order to mark, must pause the program, waits for a long time.

Second, the tag cleanup algorithm will cause memory fragmentation, such as being marked to clear only a few small blocks of memory, and we are going to apply for a few chunks of memory, then the memory just cleared out, in fact, is still unusable.

Three, copy (copying) algorithm

Mark-sweep algorithm is inefficient, and thus, a new algorithm is produced.

1) Divide the note paper into two parts

  

2) Everyone is on the left, sticky note paper used, when the leader said stop, this time do not mark, as long as with the need to transfer the note paper to the right.

  

3) The useless paper left, and then threw the paper into the dustbin, the next time the garbage collection is done in the same way, but this time from the right to the left to migrate.

  

4) The efficiency of this algorithm is very high, unfortunately, the memory consumption is too large, especially in 1960, memory can be more than hypocrite, directly cut off half of the memory, obviously unacceptable.

Resources:

Http://kb.cnblogs.com/page/74836/GC and JS memory leaks

Analysis of http://www.cnblogs.com/hyddd/archive/2013/02/07/2908598.html JavaScript garbage collection

Http://blog.jobbole.com/49084/JavaScript Garbage Collection

http://kb.cnblogs.com/page/137723/reducing JavaScript garbage collection [Translate]

JavaScript garbage Collection (ii)--garbage collection algorithm

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.