Home > Web Front-end > JS Tutorial > body text

What is js garbage collection mechanism? Introduction to js garbage collection mechanism

不言
Release: 2018-08-15 17:40:44
Original
4472 people have browsed it

The content of this article is about what is the js garbage collection mechanism? An introduction to the js garbage collection mechanism. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

JS has an automatic garbage collection mechanism. The garbage collector is executed periodically at fixed intervals.

Common garbage collection methods in Js: mark clearing and reference counting.

1. Mark clearing method:

Working principle: When a variable enters the environment, mark the variable as "entering the environment". When a variable leaves the environment, it is marked as "leaving the environment". The memory marked "leaving the environment" is recycled.

Workflow:

  • The garbage collector will mark all variables stored in memory during operation;

  • Remove the tags of variables in the environment and variables referenced by variables in the environment;

  • is marked will be treated as variables ready for deletion;

  • The garbage collector completes the memory cleanup work, destroys those marked values ​​and reclaims the memory space they occupy. .

2. Reference counting method:

Working principle: Track and record the number of times each value is referenced.

Work flow:

  • Declare a variable and assign a reference type value to the variable. The number of references to this reference type value is 1;

  • The same value is assigned to another variable, and the number of references of this reference type value is increased by 1;

  • When the variable containing this reference type value is assigned another value, then the number of references to this reference type value is reduced by 1;

  • When the number of references becomes 0, it means that there is no way to access this value.

  • #When the garbage collector runs next time, it will release the memory occupied by the value with a reference count of 0.

But when there is a circular reference, the memory cannot be released - causing a memory leak

Because the implementation of BOM and DOM in IE uses COM, and COM objects The garbage collection mechanism used is the reference technology strategy. So there will be a circular reference problem.

Solution: Manually disconnect the link between the JS object and the DOM and assign the value to null. IE9 converts DOM and BOM into real JS objects, so this problem is avoided.

Related recommendations:

PHP’s garbage collection mechanism What are the recyclable garbage Garbage collection Non-recyclable garbage

About PHP5 .3 garbage collection mechanism

The above is the detailed content of What is js garbage collection mechanism? Introduction to js garbage collection mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!