I've recently been writing quite a bit of JavaScript related to a few new features in CS2.1. Today, I reviewed my recent scripts for memory leaks. Yes, even JavaScript can suffer from memory leaks -- at least in IE 6-.
Here are a few good articles related to JavaScript memory leaks:
To summarize, JavaScript-related memory leaks occur (usually) when a JavaScript object and a DOM object have a circular reference. Consider the following code:
function JSObject(domNode)
{
this.DomNode = domNode;
this.DomNode.JSObject = this;
}
var domNode = document.getElementById('domNodeId');
var jsObject = new JSObject(domNode);
Because jsObject.DomNode references domNode and domNode.JSObject references jsObject, IE freaks out and can't reclaim this memory -- not until the browser is closed (navigating to a different page or minimizing the browser has no effect).
To resolve this issue, the script needs to either avoid this situation or clean up these references (such as setting domNode.JsObject = null and/or jsObject.DomNode = null) before the page is unloaded. For cleaning, the window.onunload event is a good option.
In my scripts the DOM tree is modified potentially often, so I clean references as necessary when changes are made and also clean *all* references when the page unloads (using window.onunload).
To check for (most) memory leaks, the Drip tool (listed above) is a good option. It allows you to navigate to a page including suspect javascript, interact with the page, and then retrieve a list of the current memory leaking issues (of the type above).