Tuesday, 7 August 2012

Firefox Memory Manager

OK, for a long time I was not happy with the huge amount of memory used by Firefox. Recently I came across a nice little application, firemin, that can be found here: http://www.datum-forensics.com/2012/05/firemin/

It is quite nice, I tried it for a couple of days. However, I was quite surprised by the huge amount of memory reduction (down to about 1 MB from 300-400 MB or more), and I thought that this does not seem normal, something else must be sacrificed. The point is, the more we reduce the memory used by firefox, the more page faults we will have.

So I wrote a small C# application to verify this and to check how many page faults I have and how much memory is used by firefox. Then I added a little bit of code to perform a "memory collection" using the EmptyWorkingSet unmanaged function (same as firemin uses), whenever the memory used by firefox reaches a certain limit which we can set. This way we can set the limit to as high as we like (for example 200 MB), depending on the available physical memory in our computer, and avoid the huge amount of page faults we get when firefox memory drops to something like 1 MB... In fact, we could just as easily let the OS handle the whole thing...

The application works on my system (windows 7) without problems (requires .NET 4.0 client profile). It is nothing extraordinary, and there are tons of things that could be improved on it, from a coding point of view. It is rather a quick hack and a proof of concept than a proper application.

Still I decided to share it here, so perhaps other people will find it useful, or decide to take the code and improve on it. One thing I was going to add if I had more time would be some real time zedgraph plot of the memory and page faults, I think that would be cool. Currently, the results can be visualised if logging is enabled and then copy/paste the resuts in excel and plot as a 2D graph. Takes a few seconds to do, and looks like this:

Please note, I have not tested it extensively and I cannot be responsible for anything that might go wrong, however, I doubt anything will, in the worst case it will probably just crash itself, as it doesn't try to do anything else other than release the memory... now if you start doing funny things like setting negative memory target and so on... you are on your own :)

In the application I used coding ideas regarding retrieving process information from a site that I can't remember now (sorry), about EmptyWorkingSet from http://forums.codeguru.com/printthread.php?t=368990

and about a task running in the tray from http://www.codeproject.com/Articles/18683/Creating-a-Tasktray-Application

(there is a more advanced tutorial on tray applications here http://www.simple-talk.com/dotnet/.net-framework/creating-tray-applications-in-.net-a-practical-guide/ but for the purposes of this application the first article on codeproject is just fine)

The application runs by default in the system tray, so you won't see anything the first time you run it. By default, it will monitor memory usage once per second. The default setting is 200 MB to perform a memory collection, and I found that it gets hit quite often with around 20 tabs open... you might want to play around. If you open the application window, you will see something like this:

The left side contains the process name (firefox.exe), process ID, number of page faults (total), working set size in bytes, average page faults in 1 sec interval over the last 60 seconds, and number of page faults in the last 1 second.

In the middle we see a log (1 line per second) of the working set size in bytes, the page faults for each second interval and the total process page faults.

On the right we see one line each time a memory collection occurs, and what was the working set size the moment the collection was triggered.

At the top right we can set the desired limit (in Mega Bytes), at the bottom left we can stop the process monitoring, and finally the Hide Window button will send the application to the tray. If you want to exit the application completely, you need to right click at the system tray icon and select "Exit".

I hope this covers the basics....

There is no license for the application, you can take it and use it for free in any way you like. If you create
some improved version, I would appreciate a post here, so that people can then know where it came from and where to get the improved version.

Here is the link to the application http://www.mediafire.com/download.php?6pafyd4d257pojy

and here is the link to the source code http://www.mediafire.com/download.php?akzcto8g3v5m5y6

Any comments are welcome, I hope this post helps someone in any way.

No comments: