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

Case Analysis of memory leak and garbage Collection Mechanism in JS

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the JS memory leak and garbage collection mechanism instance analysis of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

What is the memory leak in JS?

Memory leak: the memory space dynamically opened up by the dynamic storage allocation function is not recycled in time after use, resulting in occupying memory units all the time, and memory leaks will be caused when the number is large.

What is garbage collection in JS?

Garbage collection mechanism is to find variables that are no longer used from time to time, and then release the memory they point to to prevent memory leaks.

Summary: although garbage collection can find some local variables that are no longer used, it can not recycle global variables, Dom element references, closures, etc., so it is also an important cause of memory leaks.

Here are two algorithms for garbage collection:

I. reference count

The reference counting algorithm adopted by IE defines the standard of "memory is no longer used". When an object does not have any other variables pointing to it, it means that the object is no longer used, and garbage collection will be carried out to free memory space. For example:

/ / create an object person. Person points to a piece of memory space with + 1 references to that memory space.

Let person = {

Age: 22

Name: 'ifcode'

}

Let p = person / / two variables point to a block of memory space with 2 references

Person = 1 / / the original person object is assigned to 1, and the number of references to the object's memory space is-1

/ / but because p points to the original person object, there is only one reference to the object space, so the object will not be recycled

P = null / / the original person object has no reference and will be recycled

However, citation technology has a fatal drawback: it can not solve the problem of circular reference.

If two objects refer to each other, even if they are no longer in use, the garbage collector will not process them, resulting in a memory leak:

In the figure above, object o1 and object O2 are looped. When the code is executed, o1 and O2 are released without being referenced, but the an in o1 can be accessed through o1 and o2.an at the same time. At this time, there is the problem of circular reference. Although the o1 and O2 objects are released, the two an in the object are still referenced to each other, and the browser will mistakenly think that these two blocks of memory are still being used. This results in a memory leak.

II. Mark removal

It is mentioned above that because the reference technology has circular references, which is a hidden big bug, it is no longer used. The current garbage collection algorithm adopted by browsers is tag removal.

Mark cleanup: change the concept of "no longer use large objects" to "unreachable objects"

That is, all objects that can be reached from the root are still used, and those objects that cannot be reached from the root (in the JS global) are marked as no longer in use and will be recycled later.

These are all the contents of this article entitled "example Analysis of memory leaks and garbage Collection mechanisms in JS". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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