Click here to Skip to main content
15,904,500 members
Articles / Programming Languages / C# 4.0
Tip/Trick

Effectively Flushing Reserved Memory By Process

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
15 Jun 2014CPOL1 min read 12K   4   6
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Keyhan Iranian
Iran (Islamic Republic of) Iran (Islamic Republic of)
I'm addict of developing softwares and designing solutions for projects.
My favorite work in programming is designing Components and user controls.

Comments and Discussions

 
GeneralMy vote of 1 Pin
msm-mmn19-Jun-14 1:24
msm-mmn19-Jun-14 1:24 
QuestionAha? Pin
johannesnestler15-Jun-14 21:54
johannesnestler15-Jun-14 21:54 
AnswerRe: Aha? Pin
Amin Esmaeily16-Jun-14 0:24
professionalAmin Esmaeily16-Jun-14 0:24 
GeneralRe: Aha? Pin
johannesnestler16-Jun-14 2:10
johannesnestler16-Jun-14 2:10 
Hi,

After some reading and a quick test I verified that this trick does indeed reduce the "working set" size, but not the real used "virtual memory". So you are just telling your app to reserve less PHYSICAL memory per process resource, triggering more page faults in the end (if page was swaped to hard disk this is very slow). So the effect is (as hinted in the docu) that you are just reducing the amout of memory which will be kept in physical RAM. Therefore you see your working set consumption "grow" again, while your app is loading the pages back (until MaxWorkingSet is reached). So your solution to "counter-work" against that is just costing a lot of performance for paging and loading back. Real memory consumption is not (or only a little) reduced.

The whole topic of memory management has always been difficult, and in the modern realm of .NET with it's managed memory + changes in OS memory handling it became even harder.
As I understand it, your trick will just do the same as putting more memory pressure on your OS (this way you could force paging too, eg. by a script reserving a lot of RAM and freeing it instantly).

Although I beliefe that for cases where a lot of "smaller" resources are loaded into the "too big" pages you get some real physical memory out, but in the end this won't help to reduce the memory consumption or fight a memory leak and has an additional bad effect on process perfomance...


How sad, I thought you found the holy grail of memory management Wink | ;)
GeneralRe: Aha? Pin
dandy7216-Jun-14 4:41
dandy7216-Jun-14 4:41 
GeneralRe: Aha? Pin
Amin Esmaeily16-Jun-14 18:07
professionalAmin Esmaeily16-Jun-14 18:07 

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

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