Minimizing memory footprint
My work on ABC-tool keeps looping back to optimizing memory usage. The ABC-tool application runs on Windows PCs which means that it has a 2 GB memory limit (typically). The largest datasets I have every run across is about 150 MB in size – but this was XML-files and the data part was significantly smaller. Even though – we come across the OutOfMemoryException waaaaay too often!
How can that be? What are we doing wrong? How can we make it better?
First off I would like to make some points clear. We write our main product ABC-tool in C#.NET. The program runs in all managed code with a few exceptions. The main exit from managed code is when we send data to Microsoft Excel. The data transmitted could include as much as the entire set of data imported into our program. Beside that we have a small brake from managed code to handle various license specific features – nothing much; probably not even worth mentioning.
Running an entire program in managed code means that we generally don’t manage memory allocation and certainly not de-allocation by ourselves! A few weeks back I wrote a little tool to manage memory consumption evolving over time by different processes and the result was very upsetting! When importing data to ABC-tool the memory consumption peaks due to the internal processing of all text into numbers, dates etc. The troubling part is that right when the data import is complete the data is applied to the views of our program which uses a fair amount of memory in itself. By inserting different pauses and delays I have concluded that the memory consumption level won’t drop until after at least 2-3 seconds of runtime. This means that there is a short delay between releasing the memory and it actually being available again. I have observed that when I start allocating more memory or even do heavy computations the garbage collector refuse to run! My conclusion was that it does nothing until idle! This means that we can have out of memory errors occurring simply because the garbage collector is waiting for us to stop allocating memory – that isn’t right!
One thing that we have certainly done wrong is assuming that memory allocating and releasing was being handled for us. Actually it was – just not while we were doing other things. This means that we will have to change the way we think of our managed code and memory.
During the last few weeks I’ve tried to identify areas of the code that makes the memory consumption peak. I’ve also tried to identify strings of events each resulting in memory peaks and forced garbage collection between them. Last but not least I feel the way I think change every time I write managed code. Memory management is still an issue – I didn’t dodge it and probably never will!


