我有以下控制台应用程序,它唯一的目的是使用正在运行的机器上的所有空闲内存:
static List<string> _str = new List<string>();
static int fill = 10000;
static int _remainingMemory = 50;
static void Main(string[] args)
{
fillString();
Console.ReadLine();
}
static void fillString()
{
long count = 0;
while (true)
{
try
{
if (++count % 500 == 0 || fill == 1)
{
ShowMemory();
}
_str.Add(new string('_', fill));
}
catch (OutOfMemoryException)
{
if (fill > 1)
{
fill = 1;
}
else
{
Console.WriteLine("Not consuming memory...");
return;
}
}
}
}
private static void ShowMemory()
{
System.Diagnostics.PerformanceCounter ramCounter;
ramCounter = new System.Diagnostics.PerformanceCounter("Memory", "Available MBytes");
Console.WriteLine("Memory Remaining: {0}", ramCounter.RawValue);
}这样做消耗了大约1GB的内存,但却留下了将近10 1GB的空闲空间。所以,我的问题是,为什么这段代码不耗尽机器上的每一个字节的内存,并使其停顿呢?
我在Windows8.1下使用VS2015。
发布于 2015-07-07 16:52:04
应用程序不直接使用内存。它使用虚拟内存。操作系统将此映射到内存页。不要去想记忆。默认情况下,请考虑地址空间,因为这是应用程序可用的抽象。
如果您的应用程序是32位,它将被限制在2GB的虚拟内存默认情况下。如果它知道大地址,它可以分别访问32位Windows/64位Windows上的3 GB或4 GB。
如果应用程序是64位,可用的地址空间是8 TB。但是,这很可能受到操作系统支持页面的能力的限制(至少在一段时间内是如此)。
发布于 2015-07-07 16:53:06
Windows设置内存的每个进程限制。应用程序的虚拟地址空间限制在2GB以内,除非您在windows中指定了某些标志来覆盖此标志。
https://stackoverflow.com/questions/31274726
复制相似问题