Can closures really cause memory leaks?

Can closures really cause memory leaks?

I encountered a very controversial issue today. Share it here. I believe it will be helpful to the front-end interviewers.

The main content is developed around the following questions, and the other aspects of knowledge involved in the article will not be described.

Question: Will closures cause memory leaks?

What is a memory leak

Memory that is no longer used and is not released in time is called a memory leak. In js, in detail, it means that we can no longer refer to an object through js code, but the garbage collector thinks that this object is still being referenced, so it will not be released during recycling. As a result, the allocated memory can never be released. If this happens more and more, it will cause insufficient memory and system crashes.

Garbage collection mechanism

In order to solve the problem of memory leakage, the js engine has a garbage collection mechanism, which allows js to automatically manage the memory, reclaim the unused variables in the memory, and then release the memory space.

js uses two strategies, one is the mark removal method, and the other is the reference counting method. In fact, we only understand the implementation principle of the other two methods by judging whether the current variable is referenced. If it is not referenced, it will be explained. This variable should be recycled. How to recycle is a matter of the two strategies mentioned above.

What is a reference?

One more thing, what is a quote? The so-called reference refers to the object stored in the heap memory that you cannot directly access, but is accessed through the address of the object stored in the stack memory. Changing the address keeps the reference to the object.

It's like a box. There is a piece of candy in the box. The candy is connected to the outside of the box. If you want to open the box and take out the candy, I'm sorry, the box is locked. It is okay to take it out of it, which is equivalent to what we call a reference. Sugar is a so-called object, and a box is equivalent to heap memory.

Closures really cause memory leaks? ?

Friends who know about closures know that the function of closures is that they can access variables outside a function inside a function. This is because the internal function belongs to the same scope as the external function. The variable is accessed through the function in the closure because the internal function maintains a reference to the variable. When a click event is registered, it is a closure. When the click event is completed At the time, it still keeps a reference to the amount of change.

At this time, there were a lot of ambiguities on the Internet, saying that closures caused memory leaks, and many interviewers asked about memory leaks caused by closures. Finally, through a lot of information, I finally found out, although many blogs said they encountered closures. Most of the actual problems of package leaks are attributed to memory leaks caused by closures. In fact, the memory leak is not caused by the closure, but the reference to the variable through the function in the closure. The closure is not the real cause of the memory leak!

A large number of blog records actually encountered memory leaks caused by closures, the root cause of which is the failure to disconnect the references to variables in time, rather than the memory leaks caused by the closures registered to listen for events. If we can control the reference, we can solve the problem of memory leaks instead of throwing this pot to innocent closures.

If you are in the interview, the interviewer said that closures will cause memory leaks, you can abandon this company, because the interviewer does not understand the real reason why closures produce memory leaks.

Guess you like

Origin blog.51cto.com/15064450/2602420