Authoritative JavaScript Memory leakage pattern _ javascript skills

Source: Internet
Author: User
Authoritative JavaScript Memory leakage mode Author:
Abhijeet Bhattacharya, system software engineer, IBM India
Kiran Shivarama Sundar (kisundar@in.ibm.com), system software engineer, IBM India

May 28, 2007

If you know the cause of Memory leakage, it is quite easy to prevent it in JavaScript. In this article, the authors Kiran Sundar and Abhijeet Bhattacharya will show you all the basic knowledge of loop references in JavaScript and explain to you why they have problems in some browsers, especially when closure is integrated. After learning about the common memory leakage modes that you should pay attention to, you will also learn many methods to deal with these leaks.

JavaScript is a powerful scripting language used to add dynamic content to Web pages. It is particularly helpful for some daily tasks, such as verifying passwords and creating dynamic menu components. JavaScript is easy to learn and use, but it is easy to cause memory leakage in Some browsers. In this introductory article, we explain how JavaScript leaks are caused, demonstrate common memory leak patterns, and explain how to deal with them.

Note: This article assumes that you are very familiar with using JavaScript and DOM elements to develop Web applications. This article is especially suitable for developers who use JavaScript For Web application development. It can also be used by users who are interested in creating Web applications to provide browser support and help those who are responsible for browser troubleshooting.

Memory leakage in JavaScript

JavaScript is a garbage collection language, which means that the memory is allocated to the object based on the object creation and will be reclaimed by the browser when no reference is made to the object. The garbage collection mechanism of JavaScript is no problem, but the browser is somewhat different in the way of allocating DOM objects and Restoring memory.

Both Internet Explorer and Mozilla Firefox use reference count to process memory for DOM objects. In the reference counting system, each referenced object retains a count to learn how many objects are referencing it. If the count is zero, the object will be destroyed and the occupied memory will be returned to the heap. Although this solution is still effective in general, there are some blind spots in circular references.

What are the problems with circular references?

When two objects reference each other, a circular reference is formed. The reference count value of each object is assigned 1. In a pure garbage collection system, loop reference is not a problem: if one of the two objects involved is referenced by any other object, both objects will be collected by garbage collection. In the reference counting system, neither of these two objects can be destroyed because the reference counting cannot be zero. In a hybrid system that uses both garbage collection and reference count, leakage occurs because the system cannot correctly identify cyclic references. In this case, neither the DOM object nor the JavaScript Object can be destroyed. Listing 1 shows a circular reference between a JavaScript Object and a DOM object.

Listing 1. Loop reference causes memory leakage

      
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.