12

MSDN:

public IntPtr MaxWorkingSet { get; set; }

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..

Some 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)
        {

        }
    }
}

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

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
Andrey
  • 5,906
  • 4
  • 24
  • 30

1 Answers1

11

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.

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.

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.

Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
Sasha Goldshtein
  • 3,499
  • 22
  • 35
  • Thanks a lot! Some more questions in this case :). What can cause a significant growth of working set usage? And what will happen when it reaches the limit? Will it be OutOfMemory exception or smth else? – Andrey Jan 31 '11 at 14:50
  • When the process allocates memory, it can consume working set (RAM). However, if there's need for RAM in other processes/system components, then some of the process' working set may be paged out to disk. If you use Job Objects and run into the limit, the process is terminated and you can receive a notification that this happened. – Sasha Goldshtein Jan 31 '11 at 15:34
  • Actually that is wrong. Working Set is how much recently used memory the process expects to need in the working set, which affects L1/L2, not RAM. It is up to the OS kernel when to evict from WS. The working set is the LRU memory that you expect to be repeated in processing, for instance a small look needs little WS while a big loop requires more. Setting Working Set doesn't affect amount of RAM the process will use. – Tedd Hansen Feb 01 '11 at 08:39
  • 2
    @Tedd Hansen: I'm sorry, this is incorrect. `SetProcessWorkingSetSize` affects exactly the amount of RAM the process will use. See the documentation http://msdn.microsoft.com/en-us/library/ms686234.aspx -- as I noted, this is a recommendation to Windows which eventually decides what pages to keep in RAM, but the recommendation has nothing to do with L1/L2 caches, only with RAM vs. page file. – Sasha Goldshtein Feb 01 '11 at 13:10
  • Hi. I saw the code in the link and found it helpful. But where are you limiting the amount of RAM the process uses. I also have a usecase where we want to limit the amount of RAM used by any processes started by this process. Can you tell us how to achieve that? – It's a trap Sep 10 '17 at 14:02
  • You can use Windows job objects. They can trap a process and its future child processes and put quotas on them, including memory limits. – Sasha Goldshtein Sep 11 '17 at 15:35