Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory usage in C#

I have a program that uses threads in C#. Is there a way to know programmatically the memory usage of the application? I want to limit the spawning of threads to say 10 megabytes of memory, how would I do that?

like image 360
yoitsfrancis Avatar asked Apr 16 '09 12:04

yoitsfrancis


2 Answers

If you want the memory of the entire running process and not on a per thread basis, how about:

// get the current process
Process currentProcess = System.Diagnostics.Process.GetCurrentProcess();

// get the physical mem usage
long totalBytesOfMemoryUsed = currentProcess.WorkingSet64;

There's a whole host of other process memory properties besides WorkingSet64 check out the "memory related" ones at the following link for the one that best suit

http://msdn.microsoft.com/en-us/library/system.diagnostics.process_properties.aspx

like image 75
binarybob Avatar answered Oct 02 '22 20:10

binarybob


While I agree with the comments you've already received on your question, the use of System.Environment.WorkingSet might perhaps be an actual answer to it if you really decide to take this course of action?

like image 37
peSHIr Avatar answered Oct 02 '22 21:10

peSHIr