【发布时间】:2019-07-30 15:57:37
【问题描述】:
我创建了一个非常简单的程序,并将我的计算机设置为具有有限数量的虚拟内存。 32GB RAM 和 4GB 虚拟内存。然后我编写了 C# 代码来消耗所有的 NET 内存。
List<HeapSizeAllocation> hsaList = new List<HeapSizeAllocation>();
public class HeapSizeAllocation
{
int[] _arr;
int s1Gb = ( 1024 * 1024 * 1024) /sizeof(int); //1GB = 1024MB 1MB= 1024Kb
public void Test()
{
//Test simple array allocation
_arr = new int[s1Gb * 2];
}
}
Click()=> 重复直到 OutOfMemory 被抛出
HeapSizeAllocation hsa = new HeapSizeAllocation();
hsaList.Add(hsa);
hsa.Test();
然后我在 Windows 中打开 TaskManager,我仍然看到 21 GB 可用空间,只有 10 GB 在使用中。为什么?
【问题讨论】:
-
你是如何限制虚拟内存的?交换文件是否增长?未使用的内存(如您的数组)可以交换到磁盘。
-
系统属性\高级\性能\虚拟内存\更改\自定义大小\4Gb
标签: c# windows memory garbage-collection out-of-memory