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

What does js memory leak mean? Introduction to js memory leaks

不言
Release: 2018-08-15 17:22:26
Original
3763 people have browsed it

The content of this article is about what does js memory leak mean? The introduction to js memory leaks has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Memory leak# means that a piece of allocated memory cannot be used or recycled until the browser process ends.

The garbage collector periodically scans objects and counts the number of other objects that have references to each object. If an object has 0 references (no other objects refer to it), or the only reference to the object is circular, then the object's memory can be reclaimed.

If the first parameter of setTimeout uses a string instead of a function, it will cause a memory leak.

Closure, console log, loop (when two objects refer to each other and retain each other, a loop will occur)

Method to release memory: assign value to "null

Causes of memory leaks:

1. Memory leaks caused by unexpected global variables

Cause: Global variables will not be recycled.

Solution: Use Strict mode avoids

2. Memory leaks caused by closures

Reason: Closures can maintain local variables within the function so that they cannot be released.

Solution: The event handler function is defined externally, contacts the closure, or deletes the reference to the DOM in the external function that defines the event handler function.

3. DOM element references that are not cleaned

Cause: Although deleted elsewhere, the DOM reference still exists in the object.

Solution: Manual deletion

4. Forgotten timer or callback

Cause: Timing There is a reference to the DOM in the device. Even if the DOM is deleted, the timer is still there, so the DOM is still in the memory.

Solution: Manually delete the timer and DOM

5. Sub-elements There is a memory leak caused by reference

Reason: when the ul li in p gets this p, it will indirectly reference a certain obtained li. At this time, because p indirectly refers to li, even if li is cleared, it is still in the memory. , and as long as li is not deleted, its parent element will not be deleted.

Solution: Manually delete the situation

Related recommendations:

JavaScript Avoid memory leaks and memory management skills_javascript skills

Detailed discussion of several situations of js memory leaks_javascript skills

The above is the detailed content of What does js memory leak mean? Introduction to js memory leaks. 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!