Eric
Eric

Reputation: 170

C# Application, Monitor Memory Consumption

Is there a way I can monitor memory consumption from within my C# application? I would like to dynamically change a variable based upon the amount of memory my program is using.

I have some code completing queries to a data base and temporarily holding this data before I write it to a file. I also have code that allows the user to manually change at what point the data is dumped from memory and written to the file, but I want the program to handle this automatically.

For example; if my process reaches 700,000K memory usage (like as viewed from Task Manager, then decrement an integer and continue processing.

Thanks, Eric

Upvotes: 0

Views: 3186

Answers (3)

JeremiahDotNet
JeremiahDotNet

Reputation: 910

You can get the current memory usage this way:

var proc = System.Diagnostics.Process.GetCurrentProcess();
var mbUsed = (proc.PrivateMemorySize64/1024)/1024;

PrivateMemorySize64 is a long representing the amount of memory used in bytes.

Upvotes: 9

li-raz
li-raz

Reputation: 1696

Why not to use performance counters and monitor virtual bytes or bytes on all heaps or use the Process class and VirtualMemorySize64

Upvotes: 0

Eduardo Ghidini
Eduardo Ghidini

Reputation: 249

You can search about PerformanceCounter class.

Upvotes: 0

Related Questions