Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# memory
Is there any way to detect memory usage of a .net application while running and implement internal memory flushing/freeing (i.e. limit my application to use 500mb ram through configuration) obviously the freeing would be done by yourself.
Posted 1-Oct-12 3:42am
Mehdi Gholam236.1K
Comments
Manfred R. Bihy at 1-Oct-12 9:55am
   
Yes, there is. Come on, google broken again or what? (+3) :P You ought to know better than coming here with a "question" like that!
Mehdi Gholam at 1-Oct-12 10:12am
   
Everyone slips up every now and again :)
Manfred R. Bihy at 1-Oct-12 10:31am
   
No worries! Sh*t happens as they say. ;)

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can check the memory usage of any process:
 
If it reaches a certain size you can dispose of unwanted objects and call garbage collection.
 
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
long memory = p.PrivateMemorySize64;
if(memory > 500 * 1024 * 1024)
{
  //dispose of unwanted object
  GC.Collect();
}
 
Although from my experience GC.Collect() doesn't always work when you ask it too. The process memory may keep growing until the framework decides it need to free up some space.
  Permalink  
Comments
Mehdi Gholam at 1-Oct-12 10:14am
   
Yes, although this would imply polling, I was really after some way of getting an event or even when the GC is about to fire.
Stephen Hewison at 1-Oct-12 10:38am
   
Yes, and I hate to get almost philosophical, but don't all events begin with a poll? It's just us spoilt 3rd generation language developers aren't used to having manage it.

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

  Print Answers RSS
0 OriginalGriff 298
1 Sergey Alexandrovich Kryukov 249
2 Yogesh Kumar Tyagi 165
3 ChauhanAjay 156
4 ClimerChinna 150
0 Sergey Alexandrovich Kryukov 6,205
1 OriginalGriff 5,333
2 CPallini 2,473
3 Richard MacCutchan 1,617
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web01 | 2.8.140814.1 | Last Updated 1 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100