JavaScript and Memory Leaks

I recently finished writing HTML5 Programming with JavaScript For Dummies. This book focuses a lot of attention on working efficiently with JavaScript to produce full-fledged browser-based applications. You’ll find everything from creating simple code to update form entries to creating interesting special effects for your user interface. In fact, I spend considerable time looking at third party libraries such as jQuery and jQuery UI—an incredibly useful set of functions that can significantly reduce coding time.

One of my goals in the book is to introduce you to techniques that produce useful applications in an incredibly short time without writing bad code. The term bad code covers a lot of ground, but one of the more serious issues is one of memory leaks. Applications that have memory leaks will cause the application and everything else on the system to slow down due to a lack of usable memory. In addition, memory leaks can actually cause the application to crash or the system to freeze when all of the available memory is used up. So, it was with great interest that I read an InfoWorld article recently entitled, “Brendan Eich tells how to prevent JavaScript memory leaks.” The article contains a lot of useful advice in writing good JavaScript code that won’t cause your users heartache. Just in case you’ve never heard of him, Brendan Eich is the creator of the JavaScript language, so I’m sure he knows quite a lot about what makes the language tick.

There are a few points of interest in the article. A big one is that the memory leak you’re seeing in your application may not be due to your code—it may be caused by the browser. The potential for browser problems is an important one to keep in mind because these issues affect every application that runs, not just yours. However, when your application performs a lot of work that requires heavy memory use, the user may see your application as the culprit. It pays to track browser issues so that you can support your users properly and recommend browser updates for running your application when appropriate. For that matter, you can simply determine whether the user has one of the poorly designed browsers and tell the user to perform an update instead of running the application.

What I found curious about the article is that it doesn’t discuss other potential sources of memory leaks. For example, using the wrong third party library could cause considerable woe when it comes to memory usage (amongst other issues). Consequently, you need to research any libraries or templates that you use carefully. The libraries, templates, and other tools discussed in my book were chosen with extreme care to ensure you get the best start possible in creating JavaScript applications.

One of the reasons I find JavaScript so compelling as a language is that it has grown to include enough features to create real applications that run in just about any browser on just about any platform. The ability to run applications anywhere at any time has been a long term goal of computer science and it finally seems to be a reality at a certain level. What are your thoughts on JavaScript? Let me know at John@JohnMuellerBooks.com.