Its fair to say that the standard CPU architecture that we all use these days is 64bit.
But even though everyone has 64bit processor and 64bit windows, some processes are still 32bit.
I’m currently working on a project that uses some 32bit 3rd party package, and because of that must also be build in 32bit.
This project does a very heavy simulation and uses some very big geometric models that can take a large amount of memory.
Because of the large models that the application needs to work with, there were times that we got OutOfMemoryExceptions.
Obviously we checked for memory leaks and did some improvement and optimization is the code, but at some point you realize that if a model weigh 1.5GB you just need to find those 1.5GB to give it.
Under 64bit environment, each 64bit process is allowed to use the astronomic number of 128TB of memory.
But for 32bit process, no matter what is the environment it is running on, only 4GB of memory at most can be allocated.
This 4GB limit is actually quite confusing because 2GB are used for the USER space and the other 2GB are reserved for the KERNEL mode. so this means that your code can only allocate up to 2GB of memory.
Enabling large address awareness
Luckily we can tweak the 2GB distribution, and give the USER mode a little more space.
To do so we need to add to the compiled binary a flag that instruct the OS to give the process a large heap address, which is done by running the following command on the compiled assembly. this should be done in the command prompt (as Administrator)
editbin /LARGEADDRESSAWARE “<Assembly>”
To make this automatic, you can add the following commands to your post-build event, or in your build machine:
editbin /LARGEADDRESSAWARE “$(TargetPath)”
dumpbin /HEADERS “$(TargetPath)” | find “Application can handle large”
the first line sets the visual studio tools path, so that the editbin command will be recognized.
To check that the assembly is set to use large address sapce, run the following command
dumpbin /headers <Assembly>
The headers should contain the line “Application can handle large (>2GB) addresses”
Extending the USER mode memory in 32bit Windows
Enabling the large address awareness in the process is not enough if youre running a 32bit windows, you must also tell the OS that its OK to give more space to the USER mode for processes that request it.
NOTE: this is only relevent for 32bit Windows
useually you’ll limit the amount of memory the OS can give to the USER mode to 3GB (leaving 1GB for the KERNEL).
For this, you need to add a switch to the Windows boot by running the following command (as Administrator) in the command prompt
bcdedit /set IncreaseUserVa 3072
Then, restart your machine in order for the switch to take effect.
To check if the switch is enabled, run the bcdedit command.
You should see an output that contains the increaseuserva switch