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.
5 Answers
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.

- 131,090
- 20
- 188
- 351
-
1I think that Process.PrivateMemorySize64() is the right one to use for active memory usage. – maxfridbe Feb 24 '09 at 21:11
-
3I'm not sure when `Process.PagedMemorySize64` is useful, but it wasn't really working in my case. I'm performing a comparison before/after a bunch of objects are instantiated and a parser runs, and PagedMemorySize64 was reporting the same value before & after, whereas `GC.GetTotalMemory(true)` seems to report the changes in memory usage more effectively. – Steve Wortham Apr 18 '12 at 18:03
-
http://stackoverflow.com/questions/750574/how-to-get-memory-available-or-used-in-c-sharp – Lost_In_Library Jun 20 '12 at 06:43
-
2@SteveWortham The various process properties (eg, PagedMemorySize64) are cached, so simply accessing them again won't give you new values. Process.Refresh() is designed to blow away the cache and get new numbers, but I found that it didn't work. However, actually creating a new Process object each time (System.Diagnostics.Process.GetCurrentProcess()) did the trick. Not ideal, but does work. – coffeetocode Aug 09 '12 at 22:10
-
1I agree with Steve that (despite potential flaws as mentioned in the answer) GC.GetTotalMemory(true) seems to be the way to go. When I allocate memory locally inside a method, and return from that method, that memory is essentially freed ready to be reused, but only GC.GetTotalMemory(true) recognizes this. – Dan W Nov 11 '12 at 05:19
Process proc = Process.GetCurrentProcess();
Logger.Info(proc.PeakWorkingSet64/1024 + "kb");

- 113
- 1
- 6
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.

- 128,308
- 78
- 326
- 506
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.

- 2,946
- 18
- 17
Maybe
Windows::System::Diagnostics::ProcessDiagnosticInfo::GetForCurrentProcess();

- 10,517
- 2
- 21
- 15