Thursday, May 26, 2005 12:39 PM
codingsanity
Splodges of Wonga (RAM)
Let's discuss your application being a memory hog. A lot of people think that the CLR will “take care of memory” and that therefore they can just do what they damn well please with RAM. Whilst RAM is looked after very well by the CLR, that doesn't make it an infinite resource. I'm not going to get into disposable resources or GC here, I'm concentrating on memory and it's limits.
How much memory can your application use? You'd think that'd be a simple answer and you'd be wrong. On a 32-bit system simple logic suggests that 4GB is the amount of memory available to us. Well, sort of. However, Windows reserves 2GB of this memory space for itself. Thus, your application can only address 2GB. But, to add insult to injury, .NET steals a fair whack of this as well. The CLR does a lot of things under the hood, and the GC information can take up an awful lot of space. There's no hard and fast rule here, but generally if you stick to under 800MB you should be fine. You'll normally have between 800 MB and 1.2GB available to your application.
So what can you do about this? You can decrease the amount of memory that Windows grabs for itself. This is a two-step process. First you need to boot Windows with the /3GB option set. Secondly, you need to link your app with the LARGEADDRESSAWARE switch. For .NET applications you can set this switch by running the following command:
link -edit -LARGEADDRESSAWARE myApp.exe
from the Visual C++ bin directory.
This will now mean that Windows will only grab 1GB of the 4GB address space, giving your app a corresponding boost in the memory it can use. I haven't tested this myself, but I'd guess that you should now have access to between 1.2GB and 1.8GB.
Of course there is another option. You could just run your app on a 64 bit machine. Now the address space is 8TB. If we assume that Windows munches half of this, and .NET another half you'd still have 2TB of memory available to your app, and 2TB ought to be enough for anyone (shades of BillG's comments about 640KB come leaping to mind).
Given all this, the moment you start thinking about allocating huge amounts of memory (e.g. read an 800MB file into memory), it's time to look at other options. The GC does not make memory an ample resource, it just makes sure that you don't have to worry about freeing it. You still have to have some idea of how big your memory footprint is going to be.
Filed under: General