Garbage collection in JavaScript

Source: Internet
Author: User
Tags garbage collection

1 Reference count Garbage collection

Core: The number of times the object is referenced by a trace record. The idea is that if an object A is assigned a variable V, the reference count value of the object A is plus 1, and if the variable v is given another value, such as a= "str", the reference count value of the object A is reduced by 1. When this reference count becomes 0 o'clock, it indicates that the memory space it occupies can be recycled.

1 var a={b:4};

2 Var v=a;//at this time a reference count of 1

3 Var vv=a; At this point A's reference count value is 2

4 v=9;//a Reference count value is 1

5 vv= "hah"; the reference count value of//a is 0, and when the garbage collector next runs, A is recycled

If a circular reference appears:

1 var a={b:4,c:null};

2 var b={a:4,c:null};

3

4 a.c=b; B's Reference count value is 1

The reference count of 5 b.c=a;//a is 1, at which point they reference each other and are not recycled

2 Mark Purge

Core: When the variable enters the execution environment, it is marked as "Enter the environment", but when the variable leaves the environment, it is marked "Leave the environment". When the garbage collector encounters variables marked "Leave the environment", it reclaims the memory space they occupy.

1 function A () {

2 Var a=12; When you enter function A, the pre-resolution declares a first and is marked as "entering the environment"

5;

4//After function A has been executed, a is marked "Leave the Environment"

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.