NET 内存不足异常-使用了1.3 GB,但安装了16 GB

当应用程序的内存使用量超过1.3 GB 时,我的 C # 应用程序出现了内存不足异常。

我在一台32位机器上遇到了同样的问题,这台机器有3GB 的内存,这在当时是有意义的。但现在我升级硬件到64位机器与16 GB 内存使用高端主板和高端内存,但内存不足的异常仍然发生后1.3 GB!

我知道没有超过2GB 的单个对象,而1.3小于2GB,所以单个对象的内置 MS2GB 限制不太可能成为问题。

当一个应用程序达到一定的内存使用阈值时,似乎有某种 Windows 死机开关。那么应该有一种方法来配置这个。是不是在登记处?

如有任何帮助,我们将不胜感激!

151015 次浏览

It looks like you have a 64bit arch, fine -- but a 32bit version of the .NET runtime and/or a 32bit version of Windows.

And as such, the address space available to your process is still the same, it has not changed from your previous setup.

Upgrade to both a 64bit OS and a 64bit .NET version ;)

There is no difference until you compile to same target architecture. I suppose you are compiling for 32 bit architecture in both cases.

It's worth mentioning that OutOfMemoryException can also be raised if you get 2GB of memory allocated by a single collection in CLR (say List<T>) on both architectures 32 and 64 bit.

To be able to benefit from memory goodness on 64 bit architecture, you have to compile your code targeting 64 bit architecture. After that, naturally, your binary will run only on 64 bit, but will benefit from possibility having more space available in RAM.

Is your application running as a 64 or 32bit process? You can check this in the task manager.

It could be, it is running as 32bit, even though the entire system is running on 64bit.

If 32bit, a third party library could be causing this. But first make sure your application is compiling for "Any CPU", as stated in the comments.

As already mentioned, compiling the app in x64 gives you far more available memory.

But in the case one must build an app in x86, there is a way to raise the memory limit from 1,2GB to 4GB (which is the actual limit for 32 bit processes):

In the VC/bin folder of the Visual Studio installation directory, there must be an editbin.exe file. So in my default installation I find it under

C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\editbin.exe

In order to make the program work, maybe you must execute vcvars32.bat in the same directory first. Then a

editbin /LARGEADDRESSAWARE <your compiled exe file>

is enough to let your program use 4GB RAM. <your compiled exe file> is the exe, which VS generated while compiling your project.

If you want to automate this behavior every time you compile your project, use the following Post-Build event for the executed project:

if exist "$(DevEnvDir)..\tools\vsvars32.bat" (
call "$(DevEnvDir)..\tools\vsvars32.bat"
editbin /largeaddressaware "$(TargetPath)"
)

Sidenote: The same can be done with the devenv.exe to let Visual Studio also use 4GB RAM instead of 1.2GB (but first backup the old devenv.exe).

Its worth mentioning that the default for an 'Any CPU' compile now checks the 'Prefer 32bit' check box. Being set to AnyCPU, on a 64bit OS with 16gb of RAM can still hit an out of memory exception at 2gb if this is checked.

Prefer32BitCheckBox

If you have 32-bit Windows, this method is not working without following settings.

  1. Run prompt cmd.exe (important : Run As Administrator)
  2. type bcdedit.exe and run
  3. Look at the "increaseuserva" params and there is no then write following statement
  4. bcdedit /set increaseuserva 3072
  5. and again step 2 and check params

We added this settings and this block started.

if exist "$(DevEnvDir)..\tools\vsvars32.bat" (
call "$(DevEnvDir)..\tools\vsvars32.bat"
editbin /largeaddressaware "$(TargetPath)"
)

More info - command increaseuserva: https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/bcdedit--set

if you're searching for the tools folder in a newer Version of Visual Studio (2017, 2019), I've got it under

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\Common7\Tools

and the files' name is VsDevCmd.bat from Visual Studio 2017 on and later.

If you just want to provide access to more memory for a specific 32 bit app outside of VS, there is a useful application at https://github.com/cybertechniques/site/blob/master/analysis_tools/cff-explorer/index.md which will set the apps headers for you.

  • Download "CFF Explorer" from the NT Core site or CFF Explorer file.
  • Launch "CFF Explorer" and open the EXE (e.g. C:\Users\dennisg\AppData\Local\Apps\2.0\MHV1WDDC.958Y0ELE416.L7Y\myapp.exe)
  • List item

In the left side of the window, choose the "File Header" section under "Nt Headers" Look on the right side for the row starting with "Characteristics", and the "Click here" field. Click there. (A context menu appears for the next part). Select/enable the checkbox for "App can handle >2gb addresses" and click 'OK'. Save the file (File > Save).