views:

366

answers:

3

How can I check programmatically, how much memory my program currently has allocated ?

I need it for an ASP .NET app, but i suspect the solution is general for all types of .NET applications. Ideally, the operation to get the currently allocated amount of memory, should be fast, since I will need to query it often.

EDIT: Thanks for your answers. I will be using GC.GetTotalMemory for this task, since I need to use it for an ASP .NET application. After some experimenting, I have determined that it is fast and accurate enough for my needs. The suggestion to use Process' PrivateMemorySize64 property will also work in general; but as pointed out in the answers, for an ASP.NET application there will often be multiple AppDomain's running in one process. I need the memory total per-AppDomain.

+4  A: 

There is a GC.GetTotalMemory Method but this will only give you an approximation. Is this enough?

If you call it with GC.GetTotalMemory(false) it will be faster ...

bruno conde
+1  A: 

You can also call PrivateMemorySize64 (or Virtual if you prefer) from the Process class. However, be aware that the numbers are cached and only refreshed when you call Refresh.

Brian Rasmussen
+1  A: 

And also note that for asp.net its very common to have more than one application in one process. So that restricts the usefulness of the PrivateMemorySize64. The GC.GetTotalMemory is per app domain afaik.

Igal Serban