Garbage collection in javascript

Source: Internet
Author: User

1 reference counting garbage collection

Core: the number of times the object is referenced. The idea is that if an object A is assigned A value to the variable v, the reference count value of the object A is increased by 1. If the variable v is assigned another value, for example, if a = "str", the reference count of the object A is reduced by 1. when the reference count value is 0, it indicates that the memory space occupied by it can be recycled.

1 var A = {B: 4 };

2 var v = A; // The reference count of A is 1 at this time.

3 var vv = A; // The reference count of A is 2 at this time.

4 v = 9; // The reference count of A is 1

5 vv = "hah"; // The reference count of A is 0. When the Garbage Collector runs next time, A is recycled.

If a circular reference occurs:

1 var A = {B: 4, c: null };

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

3

4 A. c = B; // The reference count of B is 1.

5 B. c = A; // The reference count of A is 1. At this time, they reference each other and will not be recycled.

2 Mark Clear

Core: When a variable enters the execution environment, it is marked as "entering the environment", but when the variable leaves the environment, it is marked as "leaving the environment". When the Garbage Collector encounters a variable marked as "out of the environment", it recycles the memory space they occupy.

1 function (){

2 var a = 12; // when entering function A, the pre-resolution declares a first and marks it as "entering the environment"

3}

4 // After function A is executed, function a is marked as "leaving the environment".

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.