简体   繁体   中英

How to get the total allocated memory by an (ASP).NET app programatically?

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM