Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand garbage collection in JS

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand garbage collection in JS". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand garbage collection in JS".

The garbage collection mechanism in JS is necessary because strings, objects, and arrays do not have a fixed size, so when their size is known, they can be dynamically allocated for storage. JavaScript every time a string, array, or object is created, the interpreter must allocate memory to store that entity. As long as memory is allocated dynamically like this, eventually it has to be freed so that it can be reused, otherwise, javascript's interpreter will consume all available memory in the system, causing the system to crash.

Why do you need garbage collection in system JS

JS is not like CumberCraft, which has its own garbage collection mechanism. Javascript's parser can detect when a program no longer uses an object, and when he determines that an object is useless, he knows that the object is no longer needed and can free up the memory it occupies.

For example:

Vara= "helloworld"

Varb= "world"

Vara=b

/ / at this point, the "helloworld" will be released, freeing up memory for re-reference

Methods of garbage collection

Mark clear

The most common method of garbage collection in js is clearly tagged. When a variable enters the environment (for example, declaring a variable in a function), the variable is marked as "enter the environment." Logically, you can never release the memory occupied by variables that enter the environment, because as long as the execution stream enters the appropriate environment, they may be used. When a variable leaves the environment, it is marked as "leave the environment."

You can mark variables in any way. For example, you can record when a variable enters the environment by flipping a particular bit, or use a list of variables that enter the environment and a list of variables that leave the environment to track which variables have changed.

At run time, the garbage collector tags all variables stored in memory (of course, you can use any markup method). It then removes variables in the environment and variable tags that are referenced by variables in the environment. Variables that are tagged after that will be treated as variables to be deleted because variables in the environment can no longer be accessed. Finally, the garbage collector completes the memory removal, destroys those marked values and reclaims the memory space they occupy.

Reference count (uncommon)

The meaning of reference counting is to track the number of times each value is referenced. When a variable is declared and a reference type value is assigned to the variable, the number of references to that value is 1. If the same value is assigned to another variable, the number of references to the value is added by 1. On the contrary, if the variable that contains a reference to this value takes another value, the number of references to that value is subtracted by 1. When the number of references to this value becomes 0, there is no way to access the value, so the memory space it occupies can be recovered. In this way, the next time the garbage collector runs, it frees the memory occupied by values with zero references.

Thank you for reading, the above is the content of "how to understand garbage collection in JS". After the study of this article, I believe you have a deeper understanding of how to understand garbage collection in JS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report