Click here to monitor SSC

Laila Lotfi

.NET tools Brand Manager & Simple-Talk Editor

The Browser That Ate My Memory

Published Monday, January 04, 2010 12:27 PM

Have you noticed occasions when your browser suddenly starts eating memory? Even the latest versions of Firefox, Opera or Internet Explorer can cause a PC to grind to a halt by grabbing all available memory. Browsers are greedy by their nature. They grab memory for such things as the Back-Forward cache in order to speed performance. However, they also suffer from JavaScript programming errors that prevent JavaScript and DOM objects from being recycled once they have fallen out of use. This is most likely to happen if you have created reference loops via closures that involve both DOM and JavaScript objects. This normally involves event handlers, but can happen in a variety of circumstances.

JavaScript has its own built-in mark and sweep garbage collection that works pretty well. The programmer does not normally have to explicitly signal that an object needs to be recycled: it is done automatically once all objects that reference the object go out of scope and its resources are released.

The problems happen because most browsers have separate systems for recycling resources in the DOM and JavaScript. The recycling of DOM objects is usually done via a reference-counting system. This system returns the memory to the heap once the reference count falls to zero. If a circular reference is detected within either recycling system, then all is well and the circularity is detected and managed, but if it involves both JavaScript and the DOM, as when a DOM object has a reference back to a closure, such as an event handler, then the objects can't be released even after the browser has navigated away from the page that caused the problem.

The seasoned JavaScript programmer will break circular references by assigning a null to the reference to the object once the object is no longer required. However, the best solution for the JavaScript programmer is probably to use a framework such as jQuery which manually releases event handlers that are assigned via the framework. This is little consolation to the user who needs to access many sites, few of which have taken any steps to ensure that their JavaScript avoids memory leaks.

Browsers have become so central to the work we do on PCs that we have become increasingly tolerant of the demands that these applications make on PC memory. However, it seems absurd that one should need to keep closing and re-starting the browser just to prevent the memory-drain from throttling the PC. It is particularly galling when the cause is programming error.

Do you have any ideas on how the browsers excessive appetite for memory can be suppressed? It would be great to hear what you think.

Cheers,

Laila

by Laila

Comments

 

Twitter Trackbacks for Laila Lotfi : The Browser That Ate My Memory [simple-talk.com] on Topsy.com said:

January 5, 2010 1:25 PM
 

andrew.taylor@analysys.com said:

Memory-hogging seems to be a particular problem with IE8, where each browser tab runs (as I understand it) as a separate process. Looking at Task manager I've often seen multiple iexplore.exe processes each consuming hundreds of MB of real memory (and sometimes even closing some tabs doesnt help!).
January 6, 2010 3:38 AM
 

Sathyaish said:

Hi Laila,

I've been enjoying the use of Reflector for the past many years and have off late been reading your newsletter with a keen interest.

Low-level stuff always has excited me more than the series of WTFs (pun intended, WPF, WCF, WF etc.) released by Microsoft.

Since Reflector's source code is not publicly available, I would like you and your team to give us an article or two about the following. If I wanted to write something close to Reflector for my own personal gratification, where would I get started? What will I have to know, other than the use of C#? Does reflector have inbuilt compilers/translators/interpretors between IL and .NET languages, i.e. languages that target the CLR? For instance, if I write my Hello World code in C# and compile it into an assembly, and select VB.NET as the language option in the Reflector settings, it shows up my code in VB.NET. It will do this for any CLR targetting language.

I wonder how you achieve this. Do you (as in Lutz or anyone else on the team) read the MSIL spec cover to cover before you start? Do I have to write a compiler/translator from IL to a CLR targetting language?

Is there any plan for making the source code available to students or researchers or generally programmers who want to better themselves at the art of programming?


With Regards

Sathyaish
January 13, 2010 6:03 AM
You need to sign in to comment on this blog

About Laila

I'm working for the .NET Tools division at Red Gate. These days, I'm spending most of my time working on SmartAssembly (.NET obfuscator, error reporting and feature usage reporting tool), and I'm currently looking to speak to anyone who has used the product for its error reporting technology. My email address: editor@red-gate.com
<January 2010>
SuMoTuWeThFrSa
272829303112
3456789
10111213141516
17181920212223
24252627282930
31123456
How to Kill a Company in One Step or Save it in Three
 The majority of companies that suffer a major data loss subsequently go out of business. Wesley David... Read more...

Migrating from OCS 2007 R2 to Lync: Part 4
 Having migrated the rest of our users and legacy resources across and started getting ready to... Read more...

Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Seth Godin: Big in the IT Business
 Seth Godin has transformed our understanding of marketing in IT. He invented the concept of 'permission... Read more...

Using SQL Test Database Unit Testing with TeamCity Continuous Integration
 With database applications, the process of test and integration can be frustratingly slow because so... Read more...