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