Monitoring ASP.NET application memory from an application

I'm looking for a way for the application itself to control the amount of memory that it uses, so I can write it to the log file every hour or so and monitor the usage of the applications.

All of them are posted, so we can make changes to the system to see what happens, so the solution must be from within the application code.

In the future, we may use memory information to influence caching policies.

+6
source share
2 answers

Hmm, how do you need the details? If you just want to use memory, you can query GC. He knows.;)

long bytes = GC.GetTotalMemory(false); // use 'false' to not wait for next collect 

The variable "bytes" will contain the number of bytes that are currently allocated in managed memory. I'm not sure if managed memory entails the whole process or just AppDomain. You will have to test this by running multiple AppDomains in the same process and seeing if the managed memory allocation is measured through the AppDomains. If this is not the case, you can use this to measure overall memory usage in an ASP.NET application.

If you need more specific information, there is a diagnostic API for the CLR with which you could interact. There are also many memory profilers, but if they work in an ASP.NET application, I cannot say.

+5
source

Alternatively, if you want more detailed information, you can read performance counters using the System.Diagnostics.PerformanceCounter class. Here are some of the counters you can connect:

Request bytes total

Total Request Bytes

Request Timeout

Requests Fulfillment

Requests / Sec

Mistakes Total

+1
source

All Articles