Click here to Skip to main content
Licence CPOL
First Posted 1 Mar 2007
Views 36,718
Bookmarked 14 times

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

By | 1 Mar 2007 | Article
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)

About the Author

schizoidboy

Web Developer

Italy Italy

Member

http://www.codeplex.com/PublicDomain

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
AnswerNot True PinmemberMember 145798520:35 28 Apr '09  
GeneralRe: Not True Pinmemberschizoidboy5:23 6 May '09  
AnswerRe: Not True PinmemberMember 14579855:55 6 May '09  
Questionhow to use IDisposable PinmemberMember 37318356:07 20 Apr '09  
AnswerRe: how to use IDisposable Pinmemberschizoidboy9:47 20 Apr '09  
GeneralRe: how to use IDisposable PinmemberMember 373183510:47 20 Apr '09  
GeneralRe: how to use IDisposable Pinmemberschizoidboy11:25 20 Apr '09  
GeneralRe: how to use IDisposable PinmemberMember 373183518:24 20 Apr '09  
GeneralRe: how to use IDisposable Pinmemberschizoidboy4:41 21 Apr '09  
GeneralRe: how to use IDisposable Pinmemberschizoidboy4:43 21 Apr '09  
GeneralRe: how to use IDisposable PinmemberMember 37318359:11 21 Apr '09  
GeneralRe: how to use IDisposable Pinmemberschizoidboy14:25 21 Apr '09  
GeneralRe: how to use IDisposable PinmemberMember 373183519:35 21 Apr '09  
GeneralRe: how to use IDisposable PinmemberMember 37318351:06 22 Apr '09  
GeneralAccess is denied Error Pinmembercraigclark7777:33 10 Sep '08  
GeneralRe: Access is denied Error Pinmemberschizoidboy15:06 13 Sep '08  
QuestionMaximum limit? PinmemberTodd Smith8:02 1 Mar '07  
AnswerRe: Maximum limit? PinmemberRobert Rohde2:22 2 Mar '07  
AnswerRe: Maximum limit? PinmemberThe_Mega_ZZTer9:34 15 Nov '07  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 1 Mar 2007
Article Copyright 2007 by schizoidboy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid