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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14186256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:59:29  来源:igfitidea点击:

.NET Out Of Memory Exception - Used 1.3GB but have 16GB installed

c#out-of-memory

提问by Paceman

I am getting an Out Of Memory exception in my c# application when the memory usage for the application goes over about 1.3GB.

当应用程序的内存使用量超过大约 1.3GB 时,我在我的 c# 应用程序中收到内存不足异常。

I had this same problem on a 32-bit machine with 3gb of memory and it made sense back then, but now I upgraded the hardware to a 64-bit machine with 16GB memory with the high - end motherboard and RAM but the Out Of Memory exception still occurs after 1.3GB!

我在一台 3gb 内存的 32 位机器上遇到了同样的问题,当时它是有道理的,但现在我将硬件升级到 16GB 内存的 64 位机器,带有高端主板和 RAM 但内存不足1.3GB后仍然发生异常!

I know that there are no single objects over 2GB and 1.3 is less the 2GB anyway, so the in-built MS 2GB limit on a single object is not likely to be the problem...

我知道没有超过 2GB 的单个对象,无论如何 1.3 小于 2GB,因此单个对象的内置 MS 2GB 限制不太可能是问题......

It seems like there is a windows kill-switch of some sort when an app reaches a certain memory usage threshold... Then there should be a way to configure this is in the registry perhaps?

当应用程序达到某个内存使用阈值时,似乎有某种形式的 Windows 终止开关......那么应该有一种方法可以在注册表中配置它?

Any help will be greatly appreciated!

任何帮助将不胜感激!

采纳答案by Tigran

There is no difference until you compileto same target architecture. I supposeyou are compiling for 32bit architecture in both cases.

编译到相同的目标架构之前没有区别。我32在这两种情况下都在编译位架构。

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

值得一提的是,OutOfMemoryException如果您在架构和位的CLR 中2GB获得单个集合分配的内存(例如),也可以提高。List<T>3264

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

为了能够从64位架构的内存优势中受益,您必须针对位架构编译代码 64。在那之后,自然地,您的二进制文件将64在位上运行,但将受益于在 RAM 中有更多可用空间的可能性。

回答by fge

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

看起来您有一个 64 位架构,很好——但是 .NET 运行时的 32 位版本和/或 Windows 的 32 位版本。

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 ;)

升级到 64 位操作系统和 64 位 .NET 版本;)

回答by Jacco

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

您的应用程序是作为 64 位进程还是 32 位进程运行?您可以在任务管理器中查看。

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

可能是,它以 32 位运行,即使整个系统运行在 64 位上。

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.

如果是 32 位,则可能是第三方库导致了这种情况。但首先确保您的应用程序正在为“任何 CPU”编译,如注释中所述。

回答by Desty

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

如前所述,在 x64 中编译应用程序可为您提供更多可用内存。

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):

但是在必须在 x86 中构建应用程序的情况下,有一种方法可以将内存限制从 1.2GB 提高到 4GB(这是 32 位进程的实际限制):

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

Visual Studio安装目录的VC/bin文件夹中,必须有一个editbin.exe文件。所以在我的默认安装中,我在下面找到它

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.batin the same directory first. Then a

为了使程序工作,也许您必须先vcvars32.bat在同一目录中执行。然后一个

editbin /LARGEADDRESSAWARE <your compiled exe file>

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.

足以让您的程序使用 4GB RAM。<your compiled exe file>是 exe,它是 VS 在编译项目时生成的。

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

如果您想在每次编译项目时自动执行此行为,请对执行的项目使用以下 Post-Build 事件:

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

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

旁注:同样可以devenv.exe让 Visual Studio 也使用 4GB RAM 而不是 1.2GB(但首先备份旧的devenv.exe)。

回答by tonycoupland

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.

值得一提的是,“Any CPU”编译的默认设置现在选中“Prefer 32bit”复选框。设置为 AnyCPU,在具有 16gb RAM 的 64 位操作系统上,如果选中此项,仍然可以在 2gb 处遇到内存不足异常。

Prefer32BitCheckBox

Prefer32BitCheckBox

回答by Mehmet Kurt

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

如果您使用的是 32 位 Windows,则此方法在没有以下设置的情况下不起作用。

  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
  1. 运行提示cmd.exe(重要:以管理员身份运行)
  2. 输入 bcdedit.exe 并运行
  3. 查看“increaseuserva”参数,没有然后写以下语句
  4. bcdedit /set increaseuserva 3072
  5. 再次执行第 2 步并检查参数

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://docs.microsoft.com/en-us/windows-hardware/drivers/devtest/bcdedit--set

更多信息 - 命令increaseuservahttps: //docs.microsoft.com/en-us/windows-hardware/drivers/devtest/bcdedit--set