Amnesia

4/12/2011 By Frank 0 comments

Having to deal with malloc and free was not always fun in the old days. Garbage collection can be a great thing. Provided that one does not store everything in a global variable and one writes a destructor for the occasional object that grabs some system resource.

But then the confusion begins. Regularly, we hear that the Windows task manager lists quite a bit of memory for our processes, and we get asked to do something about it. Now, I am not going to deny that we can do better in this respect, in principle. The trouble with the Task Manager is that it is does not really show how much memory our component uses. It just shows the amount of “managed heap” that the memory management system of .Net fancied.

For example: suppose I take a PDF document with a number of pages that each consists of an image, and I open it in PDFControls.NET in Windows XP. After some scrolling, the task manager shows me 87 MB of memory use. Now, if I scroll some more, it increases to 120 MB. But then at some point it falls back to 87 MB again, indicating that the garbage collector has decided to collect all generations. And even then it is not certain that it has not kept around some “working space”.

If I open this document in Acrobat Reader, it shows 55 MB of memory use. This is a bit lower than PDFControls.NET, but given that PDFControls.NET is a .Net application, this is not too bad.

So, we tell customers to force a garbage collection and have the garbage collector return the memory used.

But then I compared XP (32 bit) with Windows 7 (64 bit).

On Windows 7, memory increases to around 200 MB for the same document, and never drops below 160 MB. So the minimum is roughly twice the memory use of XP.

Such are the joys of doubling the pointer size, I thought. Then I realized that the document contains only images. Most of the memory is spent on byte arrays. So why does Windows 7 take this much memory? Perhaps it is just the fact that the Windows machine has more internal memory. Or does the garbage collector use a different allocation strategy on newer OS versions?

Maybe Microsoft will tell us, one day.

As always, comments are welcome!