codeRambler Ramblings on code, the web, and technology

Memory leaks and closures in Javascript
Thursday August 02nd 2007, 11:57 am
Filed under: Javascript, Web development

The topic of memory leaks reared it’s ugly head again recently and I tracked down several good articles and tutorials that discuss and explain some of their causes (and how to prevent them) quite well.

One of the main symptoms of memory leaking is performance slowdown in the browser (this affects all browser, some to a greater degree than others). Investigations of this type of problem invariably lead to Javascript closures – a very powerful feature of Javascript that is very easy to abuse.

For those looking for an explaination of what closures (aka. continuation by some) are and how they are so important to Javascript, take a look at this informative article at jibbering.com.

There is a working account of one person’s battle with closure related memory leaks titled IE: where’s my memory?. This article links to many good resources, and has a very well thought out explaination as to why closures are so useful in the first place.

Of course there is a formal description at the MSDN Library as well (complete with diagrams, code examples and patterns).

There is a good tutorial on how to avoid Javascript memory leaks at Jack Slocum’s Blog. This specifically mentions problems experienced with the Dojo Toolkit, Script.aculo.us (along with Prototype) and even WordPress (used to power this blog).

After reading all those articles, no doubt you will be itching to refactor your existing code!



Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>