This is a mirror of official site: http://jasper-net.blogspot.com/

Out of memory? Easy ways to increase the memory available to your program

| Tuesday, October 5, 2010
When you run your VB or C# application, you might get an OutOfMemoryException thrown, even if your  machine has lots of  memory.

Every 32 bit process has a 2^32 bit (4 Gig) address space. That means every pointer has a size of 32 bits (4 bytes) and thus is limited to 4 Billion.

That’s the equivalent of saying a vehicle license plate number consists of 6 digits and thus there are 1 million possible numbers.

That 4 Gigs is divided into half: the user application gets the lower half and the OS gets the upper. (This boundary can be changed: see below).

Start VS 2010. File->New->Project->VB or C# Windows WPF Application.
Paste the VB or C# code below. It creates a heap then allocates 100Meg of memory in a loop continuously until an exception is thrown.

On my 64 bit Windows 7 machine with 8 Gigs of RAM (your digital camera or phone might have  more memory!), I get about 1.4Gig allocated before it dies.

Iter #10 1,048,576,000
Iter #11 1,153,433,600
Iter #12 1,258,291,200
Iter #13 1,363,148,800
Exception Exception of type 'System.OutOfMemoryException' was thrown.

Now choose Project->Properties->Compile->Build Events->PostBuildEvent Command and  added these 2 lines
call "$(DevEnvDir)..\..\vc\vcvarsall.bat" x86
"$(DevEnvDir)..\..\vc\bin\EditBin.exe" "$(TargetPath)"  /LARGEADDRESSAWARE

Note: the positions of the quotes are critical
The first line calls a BAT file that makes various tools available on the path.
The second runs EditBin on the target binary, using the LARGEADDRESSAWARE flag (that’s almost all left hand keys on the keyboard!)
Also uncheck the option: Project->Properties->Debug->Enable the Visual Studio Hosting Process

Read more: Calvin Hsia's WebLog

Posted via email from .NET Info

0 comments: