5,139,275 members and growing! (11,802 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » .NET Framework » General     Advanced

How to set the maximum memory a process can use at the OS level

By schizoidboy

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.
C++, C# 2.0, C#Windows, .NET, TabletPC, .NET 3.0, .NET 2.0, NT4, Win2K, WinXP, Win2003, VistaVS2005, VS, Dev

Posted: 1 Mar 2007
Updated: 1 Mar 2007
Views: 7,659
Announcements



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 1.86 Rating: 2.20 out of 5
2 votes, 28.6%
1
2 votes, 28.6%
2
2 votes, 28.6%
3
1 vote, 14.3%
4
0 votes, 0.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 method PublicDomain.Win32.Job.CreateJobWithMemoryLimits(uint minWorkingSetSize, uint maxWorkingSetSize, params Process[] processesToLimit), which 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.

http://www.codeplex.com/PublicDomain

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

schizoidboy


http://www.codeplex.com/PublicDomain
Occupation: Web Developer
Location: Italy Italy

Other popular .NET Framework articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralMaximum limit?memberTodd Smith9:02 1 Mar '07  
GeneralRe: Maximum limit?memberRobert Rohde3:22 2 Mar '07  
GeneralRe: Maximum limit?memberThe_Mega_ZZTer10:34 15 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 1 Mar 2007
Editor:
Copyright 2007 by schizoidboy
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project