The garbage collection mechanism of JavaScript

Source: Internet
Author: User
Tags garbage collection

Original

https://www.jianshu.com/p/4aa1a29781cc

Outline

1. Understanding garbage collection mechanism
2. Principle of garbage collection mechanism
3. The labeling strategy of garbage collection mechanism
4. Garbage collection mechanism and memory management

1. Understanding garbage collection mechanism

JavaScript has an automatic garbage collection mechanism, which means that the execution environment is responsible for managing the memory that the code uses during the execution of the environment.
The garbage collector periodically performs this operation at a fixed time interval (or a predetermined collection time in code execution).
The purpose of the garbage collection mechanism is to release the memory that is consumed by a variable that is no longer in use.
The garbage collection mechanism exists so that memory is not always consumed and not released, so that the entire system cannot support larger operations.
Garbage collection is not always efficient, it's not always running, so it's sometimes necessary for us to artificially free up the occupied memory, so that the program can support a larger operation and be able to accept more data operations.

2. Principle of garbage collection mechanism

The principle of the garbage collection mechanism: Identify variables that are no longer in use, and then release the memory they occupy.

3. The labeling strategy of garbage collection mechanism

The garbage collector must keep track of which variables are useful and which ones are useless, marking variables that are no longer useful for future recall of their occupied memory. The policy used to identify the useless variable may vary by implementation, but there are typically two policies that are specific to the implementation in the browser: Tag purge, reference count.

3.1. Mark Clear

When a variable enters the environment, it is marked as "going into the environment", and when the variable leaves the environment, it is marked as "out of the Environment" (common).

3.2. Reference counting

Trace records the number of times each value is referenced. When declaring a variable and assigning a reference type to the variable, the value reference is 1, and if the same value is assigned to another variable, the number of references to that value is added 1, instead, if the variable containing the value gets another value (that is, the previous reference is overwritten and no longer references the previous value), The number of references to this value is reduced by 1. When the number of references to this value becomes 0 o'clock, it means that there is no way to access the value again, so that it can reclaim the memory space it occupies. (Rare, almost no, except for the early version of the IE element JS, because there will be a circular reference problem).

4. Garbage collection mechanism and memory management

The garbage collector runs periodically, and if the amount of memory allocated to a variable is considerable, the amount of recycling is considerable. The improvement of the triggering method is very important.

4.1. Managing Memory

Although the use of a garbage-collected language to write programs, developers generally do not worry about memory management issues. But the problem with JavaScript in memory management and garbage collection is a bit different, one of the main problems is to prevent the Web page running JavaScript from exhausting all of the system memory and causing the system to crash for security reasons. So the amount of memory allocated to the Web browser is usually less than the program assigned to the desktop app.

4.2. Optimization

Make sure that the least amount of memory is used to get better performance for your page. The best way to optimize memory footprint is to have the code in execution save only the necessary data. Once the data is no longer useful, it is best to release its reference by setting its value to null-this is called dereferencing. This practice applies to most global variables and properties of global objects. Local variables are automatically dereferenced when they leave the execution environment.
Releasing a reference to a value does not mean that the memory consumed by the value is automatically reclaimed. The real effect of dereferencing is to leave the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs.

function Createperson (name) {    var Localperson = new Objcet ();    Localperson.name = name;    return Localperson;} var Globalperson = Createperson ("Nicholas");//Manual de-globalperson reference globalperson = null;

  

The garbage collection mechanism of JavaScript

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.