当我有足够的内存时,为什么会出现 OutOfMemory 异常?
Why am I getting OutOfMemory exceptions when I have plenty of memory?
我有一个执行循环的程序 (Fractal10),其中迭代次数取决于我手动设置的参数。当迭代次数较少时,程序运行良好。当迭代次数很大时,出现以下错误:
Unhandled Exception: System.TypeInitializationException: The type initializer for '<StartupCode$Fractal10>.$Program' threw an exception. ---> System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
我是 运行 Windows 10 Professional,拥有 16 GB 内存和 25600 MB 虚拟内存,使用 Intel Haswell 处理器和最新版本的 Microsoft Visual Studio 2017。之后抛出异常,任务管理器显示如下:
Memory: 64%
Fractal10 (32 bit)L 1,719.2 MB # this is the culprit
Microsoft Visual Studio 2017 (32 bit): 822.7 MB
... # other apps
为什么只使用了 64% 的内存时会出现此错误?我能做些什么吗?
首先,您的 OS 是 32 位还是 64 位?如果是前者,您实际上无法访问所有已安装的内存。
此外,(我假设您使用的是 .Net 框架,如果我错了请纠正我)您需要在构建配置中将平台目标设置为 x64。除此之外,您可以将以下配置添加到您的 app.config 文件中:
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
这将允许遍历大于 2GB 的对象。
如果您打算使用超过 ~1.8 GB 的内存,您应该使用 64 位版本的软件。
我有一个执行循环的程序 (Fractal10),其中迭代次数取决于我手动设置的参数。当迭代次数较少时,程序运行良好。当迭代次数很大时,出现以下错误:
Unhandled Exception: System.TypeInitializationException: The type initializer for '<StartupCode$Fractal10>.$Program' threw an exception. ---> System.AggregateException: One or more errors occurred. ---> System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
我是 运行 Windows 10 Professional,拥有 16 GB 内存和 25600 MB 虚拟内存,使用 Intel Haswell 处理器和最新版本的 Microsoft Visual Studio 2017。之后抛出异常,任务管理器显示如下:
Memory: 64%
Fractal10 (32 bit)L 1,719.2 MB # this is the culprit
Microsoft Visual Studio 2017 (32 bit): 822.7 MB
... # other apps
为什么只使用了 64% 的内存时会出现此错误?我能做些什么吗?
首先,您的 OS 是 32 位还是 64 位?如果是前者,您实际上无法访问所有已安装的内存。
此外,(我假设您使用的是 .Net 框架,如果我错了请纠正我)您需要在构建配置中将平台目标设置为 x64。除此之外,您可以将以下配置添加到您的 app.config 文件中:
<runtime>
<gcAllowVeryLargeObjects enabled="true" />
</runtime>
这将允许遍历大于 2GB 的对象。
如果您打算使用超过 ~1.8 GB 的内存,您应该使用 64 位版本的软件。