« Creating a Word Add-in using Visual Studio 2005 beta 2 | Main | Technorati Profile Link »

Avoiding Out of Memory Exceptions in .NET

Updated 2005-11-16 15:33 Wed

One solution to automatically setting the /LARGEADDRESSAWARE switch in the copy of an exe that gets built into a Windows Forms setup file is to add the following line to the project’s post-build event:

editbin /LARGEADDRESSAWARE "$(ProjectDir)obj\Debug\$(TargetFileName)"

This appears to work because the setup project takes its copy of the exe (primary project output) from under the “obj” folder, and not the “bin” folder.

Updated 2005-11-15 08:57 Tue

Getting access to more memory for your .NET 2.0 windows forms applications using the /LARGEADDRESSAWARE switch is described below, but there’s a problem with distributing applications that use this switch.

If you use the method described below to automatically set the switch each time you build a project in Visual Studio 2005, you will discover that it fails to set the switch in executables included in setup projects.

It appears that project build events are not executed as part of building a setup project that includes their primary outputs.

 

Running a simple .NET (2.0 beta 2) application that allocates 1MB buffers until the OutOfMemoryException is thrown gives me these results (on Windows XP SP2):

Scenario

Successful Allocations

Default configuration, running under Visual Studio 2005 beta 2

1605 MB

Default configuration, running from the command line

1704 MB

With /3GB and /LARGEADDRESSAWARE switches

2685 MB

 

A “pure” .NET application appears to be “largeaddressaware”-safe. If your application uses third party libraries, they may not like receiving addresses that exceed 2GB.

Making maximum use of RAM on a Windows XP system with more than 2GB or RAM requires a few extra steps:

·        /3GB Switch

Windows XP looks for a /3GB switch in the boot.ini file when it loads to determine whether to partition the 4GB address space as 2GB user / 2GB system (the default), or as 3GB user / 1GB system.

To turn on this switch:

o       Control PanelàSystemàAdvancedàStartup and Recovery SettingsàEdit

o       Add “ /3GB” to the end of the line following the “[operating systems]” section.

o       Restart the machine for the change to take effect.

·        /LARGEADDRESSAWARE Switch

An executable must have this switch enabled to make use of the extra memory beyond 2GB.

To turn on this switch:

o       Add to PATH environment variable:

§         c:\Program Files\Microsoft Visual Studio 8\common7\ide

§         c:\Program Files\Microsoft Visual Studio 8\vc\bin

o       In Visual Sutdio, add a Post-Build Event:

§         editbin /LARGEADDRESSAWARE “$(TargetPath)”

Links:

http://www.dotnet247.com/247reference/msgs/55/276803.aspx

 

 

Comments

Do you know if this works with ASP.NET processes? The executable is really aspnet_wp.exe (IIS5) or w3wp.exe (IIS6), not an .exe that I created, such as in a Windows Forms or Console app. TIA.
Allen

The principles will be the same but the details are all different :-)

Sorry I haven't had to do this with ASP.NET yet.

Compile a 32 bit app with /LARGEADDRESSAWARE switch
and run it on a 64bit cpu should give you 4 GB.

http://support.microsoft.com/kb/889654

I have been trying to track this down since hearing about it on The .NET Show: Win64

Will this work?

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)