Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Effectively Flushing Reserved Memory By Process

0.00/5 (No votes)
15 Jun 2014 1  
This trick is about how to effectively reduce reserved memory by process.

Introduction

In this trick, I'm going to describe a short but effective way to reduce assigned memory to our process.

Background

All the time, I was looking for a good way to decrease allocated memory to .NET applications. I used some methods such as GC.Collect() and IDisposable interface to dispose reserved memory by application, but none of them helped me. Finally, I found System.Diagnostics.Process.MinWorkingSet property that works perfectly.

Using the Code

MinWorkingSet is a public property of System.Diagnostics.Process class. By using this property, we can get or set minimum size of the allocated memory to process. But in this way, it works opposite of its name.

For decreasing memory of current process, we should set a value (e.g. 3000) to MinWorkingSet property like below:

try
{
      System.Diagnostics.Process.GetCurrentProcess().MinWorkingSet = (IntPtr)3000;
}
catch
{
}

After setting this value, you can see the allocated memory to application decreased to 3000KB ~ 4000KB. But after some seconds, it grows to about 7000KB ~ 8000KB again. For using this trick as well as you want, you should use a timer and call this code in each period of time (e.g. 30s).

I hope this trick helps you my dear friends. I apologise for my weak English language.

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