Click here to Skip to main content
11,415,734 members (84,844 online)
Click here to Skip to main content

How to Set the Maximum Memory a Process can Use at the OS Level

, 1 Mar 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Describes how to use WIN32 methods, and the concepts of Jobs to set a hard limit on the amount of memory a process can use. Also, provides a C# wrapper around the PInvoke calls.

Introduction

It is not a common requirement, but I ran into a scenario where I needed to set a hard Operating System limit on the maximum amount of memory a process could use. The Process.MaxWorkingSet property is, from what I can gather, merely a suggestion and easily worked-around by the Process.

This article describes the general approach to setting a hard limit to the memory a process can use and provides a link with running code to do so.

Approach

Windows has the concept of Job Objects, allowing management of system resources at an OS level, such as CPU or memory requirements.

To set a hard memory limit, the process is simple:

  1. Create a Job object using CreateJobObject
  2. Use the SetInformationJobObject method to set the various process limits and controls, such as memory
  3. Finally, assign a running process to the Job with AssignProcessToJob and it will automatically be held to the limits of the associated Job.

This code is demonstrated in the PublicDomain package in the following method:

PublicDomain.Win32.Job.CreateJobWithMemoryLimits
    (uint minWorkingSetSize, uint maxWorkingSetSize, params Process[] processesToLimit)

This allows you to pass the min/max memory limits and a list of running Processes to apply the limit to.

You can watch the memory rise in task manager and hit the maximum that you set, subsequently throwing OutOfMemory Exceptions (if the GC can't reclaim enough needed memory).

The code returns a C# Job object which is a light wrapper around the handle returned by the WIN32 CreateJobObject method. The C# class also implements IDisposable, and once the Job is disposed, the memory limits are removed:

using (Process p = new Process())
{
    p.StartInfo = new ProcessStartInfo("myexe.exe");
    p.Start();

    using (PublicDomain.Win32.Job j = PublicDomain.Win32.Job.CreateJobWithMemoryLimits(
        (uint)GlobalConstants.BytesInAMegabyte * 12,
        (uint)GlobalConstants.BytesInAMegabyte * 30,
        p))
    {
        // As long as the Job object j is alive, there
        // will be a memory limit of 30 Mb on the Process p
    }
}

PublicDomain

The PublicDomain package is a completely free, public domain collection of oft-needed code and packages for .NET. There is no license, so the code (or any part of it) may be included even in corporate applications. Public domain code has no authority.

History

  • 1st March, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

schizoidboy
Web Developer
Italy Italy
http://www.codeplex.com/PublicDomain

Comments and Discussions

 
Questionis this working on windows8 64 bit? Pin
AnandChavali3-May-14 23:01
memberAnandChavali3-May-14 23:01 
AnswerNot True Pin
Member 145798528-Apr-09 21:35
memberMember 145798528-Apr-09 21:35 
GeneralRe: Not True Pin
schizoidboy6-May-09 6:23
memberschizoidboy6-May-09 6:23 
AnswerRe: Not True Pin
Member 14579856-May-09 6:55
memberMember 14579856-May-09 6:55 
Questionhow to use IDisposable Pin
Member 373183520-Apr-09 7:07
memberMember 373183520-Apr-09 7:07 
AnswerRe: how to use IDisposable Pin
schizoidboy20-Apr-09 10:47
memberschizoidboy20-Apr-09 10:47 
GeneralRe: how to use IDisposable Pin
Member 373183520-Apr-09 11:47
memberMember 373183520-Apr-09 11:47 
GeneralRe: how to use IDisposable Pin
schizoidboy20-Apr-09 12:25
memberschizoidboy20-Apr-09 12:25 
GeneralRe: how to use IDisposable Pin
Member 373183520-Apr-09 19:24
memberMember 373183520-Apr-09 19:24 
GeneralRe: how to use IDisposable Pin
schizoidboy21-Apr-09 5:41
memberschizoidboy21-Apr-09 5:41 
GeneralRe: how to use IDisposable Pin
schizoidboy21-Apr-09 5:43
memberschizoidboy21-Apr-09 5:43 
GeneralRe: how to use IDisposable Pin
Member 373183521-Apr-09 10:11
memberMember 373183521-Apr-09 10:11 
GeneralRe: how to use IDisposable Pin
schizoidboy21-Apr-09 15:25
memberschizoidboy21-Apr-09 15:25 
GeneralRe: how to use IDisposable Pin
Member 373183521-Apr-09 20:35
memberMember 373183521-Apr-09 20:35 
GeneralRe: how to use IDisposable Pin
Member 373183522-Apr-09 2:06
memberMember 373183522-Apr-09 2:06 
GeneralAccess is denied Error Pin
craigclark77710-Sep-08 8:33
membercraigclark77710-Sep-08 8:33 
GeneralRe: Access is denied Error Pin
schizoidboy13-Sep-08 16:06
memberschizoidboy13-Sep-08 16:06 
QuestionMaximum limit? Pin
Todd Smith1-Mar-07 9:02
memberTodd Smith1-Mar-07 9:02 
AnswerRe: Maximum limit? Pin
Robert Rohde2-Mar-07 3:22
memberRobert Rohde2-Mar-07 3:22 
AnswerRe: Maximum limit? Pin
The_Mega_ZZTer15-Nov-07 10:34
memberThe_Mega_ZZTer15-Nov-07 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.4 | Last Updated 1 Mar 2007
Article Copyright 2007 by schizoidboy
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid