Click here to Skip to main content
15,881,844 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone.

Based on system.diagnostics.process, I wonder if it would not be possible to monitor the memory usage of a process and determine if the memory is growing not as expected ?
This is to be able to invoke the gc.collect() at specific memory conditions.

What is your view on that ?
Thank you very much in advance.
Best regards.
MiQi

What I have tried:

I have currently used gc.Collect but I would like to tune it based on the working memory status.
Posted
Updated 31-Aug-16 5:06am
Comments
Andy Lanng 31-Aug-16 8:59am    
Garbage is collected pretty regularly without prompt. It's pretty hard to leak memory in .Net, I mean, you REALLY have to try to leak memory.
You can inspect the app as a running process, but you'd have to poll the process to get updates on it's current usage.

Fundamentals of Garbage Collection
SuperMiQi 31-Aug-16 9:13am    
Hello Andy,
Thank you very much for your reply.
I have currently a monitoring process running while my main application is running.
I was thinking to add a method in this monotoring app to check if the working set and the private memory is behaving properly. When a criteria is reached I could then invoke gc.collect.

Maybe I am thinking too far.

What do you think?
Thank you very much in advance.
Best regards.
MiQi
[no name] 31-Aug-16 9:46am    
Why do you think that you need to mess with the GC at all? It's very good at it's job having been optimized over the years. There should be no reason at all for you to be messing with it. Just let it do it's job.
Philippe Mori 31-Aug-16 10:06am    
Fix your memory leaks if you have some (by using a memory profiler) and as already suggested, don't mess with the garbage collector...
SuperMiQi 31-Aug-16 15:25pm    
Thank you for your great inputs.

DO NOT CALL GC.Collect(). I guarantee you that you don't know anything more about the behavior of your applications memory habits over and above what the GC knows about your app.

The garbage collector is very good at it's job and is self-tuning. It learns about your applications allocation behavior and tunes itself for the best memory management and performance. By calling GC.Collection(), you are actually going against that data and reducing the performance of your app.

Also, forcing a Collect does NOT fix memory leak issues. Figure out what's leaking and fix the problem instead. .NET Memory Profilers are the best tools for this job.

Also, the counters you see in Task Manager are LYING to you. That is not how much memory your app is actually using. It's how much the .NET CLR as RESERVED for your application. The .NET CLR allocates blocks of memory from Windows and adds them to the Managed Heap. (This is what you see in Task Manager.) As your application is loaded and runs, objects are allocated from the Managed Heap. As objects go out of scope, the GC reclaims that memory as it's returned back to the Managed Heap, NOT TO WINDOWS.

Now, if Windows wants memory back, it just asks the .NET CLR to return whatever it can and the CLR does this very nicely.

All of this is done in the background without you screwing around with it.
 
Share this answer
 
something likes this:
C#
Process myProcess = Process.GetProcessById(pid);
var memoryUsage = Math.Max(myProcess.WorkingSet64, myProcess.PrivateMemorySize64);
var memoryMb = (int)(memoryUsage / (1024L * 1024L));
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900