04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4: Variables, Scope, and Memory<br />

Reference counting was initially used by Netscape Navigator 3.0 and was immediately met with a<br />

serious issue: circular references. A circular reference occurs when object A has a pointer to object B and<br />

object B has a reference to object A, such as in the following example:<br />

function problem(){<br />

var objectA = new Object();<br />

var objectB = new Object();<br />

}<br />

objectA.someOtherObject = objectB;<br />

objectB.anotherObject = objectA;<br />

In this example, objectA and objectB reference each other through their properties, meaning that each<br />

has a reference count of two. In a mark - and - sweep system, this wouldn ’ t be a problem because both<br />

objects go out of scope after the function has completed. In a reference - counting system, though, objectA<br />

and objectB will continue to exist after the function has exited because their reference counts will never<br />

reach zero. If this function were called repeatedly, it would lead to a large amount of memory never being<br />

reclaimed. For this reason, Netscape abandoned a reference -counting garbage-collection routine in favor<br />

of a mark - and - sweep implementation in version 4.0. Unfortunately, that ’ s not where the reference -counting<br />

problem ended.<br />

Not all objects in IE are native JavaScript objects. Objects in the Browser Object Model (BOM) and<br />

Document Object Model (DOM) are implemented as COM (Component Object Model) objects in C++,<br />

and COM objects use reference counting for garbage collection. So even though the IE JavaScript engine<br />

uses a mark - and - sweep implementation, any COM objects that are accessed in JavaScript still use<br />

reference counting, meaning circular references are still a problem when COM objects are involved. The<br />

following simple example demonstrates a circular reference with a COM object:<br />

var element = document.getElementById(“some_element”);<br />

var myObject = new Object();<br />

myObject.element = element;<br />

element.someObject = myObject;<br />

This example sets up a circular reference between a DOM element ( element ) and a native JavaScript<br />

object ( myObject ). The myObject variable has a property called element that points to element , and<br />

the element variable has a property called someObject that points back to myObject . Because of this<br />

circular reference, the memory for the DOM element will never be reclaimed even if it is removed from<br />

the page.<br />

To avoid circular - reference problems such as this, it ’ s best to break the connection between native<br />

JavaScript objects and DOM elements when you ’ re finished using them. For example, the following code<br />

cleans up the circular references in the previous example:<br />

myObject.element = null;<br />

element.someObject = null;<br />

Setting a variable to null effectively severs the connection between the variable and the value it<br />

previously referenced. The next time the garbage collector runs, these values will be deleted and the<br />

memory will be reclaimed.<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!