Tech Journal Back to Tech Journal

All my program does is display "Hello World". Why does it take up multiple MBytes of memory?

You can make your Windows programs seem like they take less memory by minimizing their working set. Actually, that's a "lie-for-children", but a good lie to start from! In reality, it almost the opposite.

Modern operating systems employ mechanisms to map between a large virtual memory space and the actual RAM physically available. This is done using "paging." This is the process of splitting both the physical and the virtual memory-space into blocks of the same size (for instance, 4Kbytes.) The trick comes in when you want to run more programs than your physical memory can hold. What the operating system will do, is "page-out" the blocks of memory that haven't been used in a while, thus freeing up physical RAM to run more programs.

That's how virtual memory works. A part of that is also to allocate chunks of memory to programs. It's a little wasteful to keep allocating and de-allocating memory, if the keeps using more memory and then less. So the operating system just leaves all of them with the program, only removing them from what's called the program's "working set" when memory is low. My point is, there's usually a lot of slack between the memory actually in use and the memory in the working set.

There's a way in Windows to remove this slack, minimizing the working set:

SetProcessWorkingSetSize(GetCurrentProcess(), (SIZE_T)-1, (SIZE_T)-1);

Only users with PROCESS_SET_QUOTA rights (Power Users and Administrators) can do this. For best results, do this when you have the absolute minimum of Windows' controls in view - menus, windows, icons, dialogs - they all take many KBytes of memory!
(This doesn't actually save memory, it just makes people think you did. Actually, it's very inefficient to do.)

Last updated on 2007-08-01 15:01:16 -0700, by Shalom Craimer

Back to Tech Journal