首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用备用内存

无法使用备用内存
EN

Stack Overflow用户
提问于 2015-07-07 16:48:37
回答 2查看 49关注 0票数 0

我有以下控制台应用程序,它唯一的目的是使用正在运行的机器上的所有空闲内存:

代码语言:javascript
复制
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。

EN

Stack Overflow用户

发布于 2015-07-07 16:53:06

Windows设置内存的每个进程限制。应用程序的虚拟地址空间限制在2GB以内,除非您在windows中指定了某些标志来覆盖此标志。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31274726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档