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

Explanation of garbage Collection Mechanism and memory leakage in JavaScript

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

Share

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

This article introduces the relevant knowledge of "JavaScript garbage collection mechanism and memory leak explanation". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Two commonly used algorithms:

Reference count (the new version of the browser has been deprecated, the reason: circular references will occur, garbage collection cannot be carried out, resulting in memory leakage)

Mark clear

Citation counting method

Reference counting, as the name implies, whether an object has a reference to it, that is, to see if there is an address in the stack pointing to the block heap memory to be freed. If not, the block memory is not needed and can be freed, that is, garbage collection.

Here is a brief example of the boss to illustrate the situation.

/ / create an object person with two references to attributes age and name var person = {age: 12, name: 'aaaa'}

Person.name = null; / / although name is set to null, because the person object also has a reference to name, name will not recycle

Var p = person; person = 1; / / the original person object is assigned to 1, but because there is a new reference p pointing to the original person object, it will not be recycled

P = null; / / the original person object is no longer referenced and will be recycled soon

Disadvantages: there is a fatal problem with reference counting, which is circular references.

When two objects refer to each other, although they are no longer in use, the garbage collector does not recycle, which may eventually lead to a memory leak.

Function cycle () {var o1 = {}; / / 1 var o2 = {}; / / 1o1.a = O2. A = o2. A = o1. A = o1; / 2 return "cycle reference!"}

Cycle ()

After the execution of the cycle function, objects o1 and O2 are actually no longer needed, but according to the principle of reference counting, references between them still exist, so this part of the memory will not be reclaimed. So modern browsers no longer use this algorithm.

But IE is still used.

Var div = document.createElement ("div"); div.onclick = function () {console.log ("click");}

The above is common, but the example above is a circular reference.

The variable div has a reference to the event handler, and the event handler also has a reference to div, because the div variable can be accessed within the function, so circular references occur.

Mark cleanup (commonly used)

What is written in the article is that the tag removal algorithm defines "objects that are no longer in use" as "objects that are unreachable". That is, starting from the root (that is, the global object in JS), the objects in memory are scanned regularly, and those objects that can be reached from the root are retained. Objects that are not reachable from the root are marked as no longer in use and are recycled later.

I personally understand here that objects that are not on the prototype chain and cannot be found from the global object chain will be considered unreachable objects (or I may have misunderstood it myself, forgetting the reader to point out), such as the following example

Var a = {} / / A here is a = null// on the global object, where the address that points to {} before a becomes null//. At this time, {} cannot be found, and it is impossible to find a through the global object, so {} will be garbage collected.

Untouchable objects contain the concept of unreferenced objects, but vice versa.

So the above example can be correctly recycled.

So now for mainstream browsers, garbage collection can be carried out simply by cutting off the connection between the object that needs to be recycled and the root.

Here is the example of the boss.

The most common memory leaks are generally related to DOM element bindings:

Email.message = document.createElement ("div"); displayList.appendChild (email.message)

/ / clear the DOM element displayList.removeAllChildren () from displayList later

In the above code, the div element has been cleared from the DOM tree, but the div element is also bound in the email object, so if the email object exists, the div element will always be kept in memory

This is the end of the introduction to the garbage collection mechanism and memory leak of JavaScript. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report