views:

2025

answers:

4

I have an app that, while running, needs to poll its own memory usage. It would be ideal if it could list out the memory usage for each object instantiated. I know this can be achieved by WMI, but I was hoping for something that doesn't rely on WMI.

+1  A: 

You could listen on perfmon counters, which will give you plenty of data (GC activity / physical memory usage / managed heap etc..)

If you need to go deeper you will probably have to attach a debugger to yourself, which is really super tricky cause you will have to spawn a new process and communicate with it, and walk through your memory.

Sam Saffron
+1  A: 

You can get some coarse granularity about your process from System.Diagnostics, the Process class. http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx.

None of the 'per object' stuff, but at least some memory info about your process can be gleaned.

Joe
+10  A: 

Two functions you might find useful are:

  GC.GetTotalMemory();
  Process.PagedMemorySize64();

My experience has been that GC.GetTotalMemory() is not terribly reliable. It often reports memory usage that is much smaller than the actual memory usage. I've seen it report that I'm using only 8 gigabytes when my program runs out of memory on a 16 gigabyte machine.

I haven't yet tested Process.PagedMemorySize64, although it does look promising.

Jim Mischel
I think that Process.PrivateMemorySize64() is the right one to use for active memory usage.
maxfridbe
+1  A: 
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");