与MaxWorkingSet限制进程内存 [英] Limiting process memory with MaxWorkingSet

查看:1065
本文介绍了与MaxWorkingSet限制进程内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN:

公众的IntPtr MaxWorkingSet {获得;组; }

获取或设置允许的最大   工作集大小相关   处理。属性值:最大   允许在工作集大小   存储器的过程中,以字节为

Gets or sets the maximum allowable working set size for the associated process. Property Value: The maximum working set size that is allowed in memory for the process, in bytes.

所以,据我了解,我可以限制内存,可使用的处理量。我想这一点,但没有运气。

So, as far as I understand, I can limit amount of memory that can be used by a process. I've tried this, but with no luck..

有些code:

public class A
{
    public void Do()
    {
        List<string> guids = new List<string>();
        do
        {
            guids.Add(Guid.NewGuid().ToString());
            Thread.Sleep(5);
        } while (true);
    }
}


public static class App
{
    public static void Main()
    {
        Process.GetCurrentProcess().MaxWorkingSet = new IntPtr(2097152);
        try
        {
            new A().Do();
        }
        catch (Exception e)
        {

        }
    }
}

达到2MB的限制后,我期待OutOfMemory例外,但没有任何反应。如果我打开任务管理器,我可以看到,内存我的应用程序采用的是不断增长的continiously没有任何限制。

I'm expecting OutOfMemory exception after the limit of 2mb is reached, but nothing happens.. If I open Task Manager I can see that the amount of memory my application uses is growing continiously without any limits.

我是什么做错了吗? 在此先感谢

What am I doing wrong? Thanks in advance

推荐答案

没有,这不限制使用的内存处理量。它只是围绕 SetProcessWorkingSetSize 的包装这一个)是一个建议,和b)限制的工作集的过程中,这是量物理的内存(RAM),这个过程会消耗。

No, this doesn't limit the amount of memory used by the process. It's simply a wrapper around SetProcessWorkingSetSize which a) is a recommendation, and b) limits the working set of the process, which is the amount of physical memory (RAM) this process can consume.

这是绝对的没有的会导致内存溢出异常在这一过程中,即使分配显著比你设定 MaxWorkingSet 属性到。

It will absolutely not cause an out of memory exception in that process, even if it allocates significantly more than what you set the MaxWorkingSet property to.

还有一个另外的你正在尝试做的事情 - 在Win32作业对象API。还有对于在codePLEX的托管包装( HTTP://jobobjectwrapper.$c$cplex.com/ ),而我做出了贡献。它允许你创建一个进程,并限制内存此过程中可以使用的金额。

There is an alternative to what you're trying to do -- the Win32 Job Object API. There's a managed wrapper for that on Codeplex (http://jobobjectwrapper.codeplex.com/) to which I contributed. It allows you to create a process and limit the amount of memory this process can use.

这篇关于与MaxWorkingSet限制进程内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆