Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring ASP.NET application memory from within the application

Tags:

memory

asp.net

I'm looking for a way for the application itself to monitor the amount of memory it is using, so I can record it in a log file every hour or so and keep an eye on the applications usage.

Its all hosted so we can make changes to the system to see what is going on so the solution will have to be from within the application code.

We may in future use the memory information to affect the caching policies.

like image 804
Simon Farrow Avatar asked Feb 25 '09 15:02

Simon Farrow


2 Answers

Hmm, how detailed information do you need? If you just want the memory usage you can ask the GC. It knows. ;)

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

The variable 'bytes' will contain the number of bytes currently allocated in managed memory. I'm not sure whether the managed memory entails the entire process or just the AppDomain. You'll have to test this by running several AppDomains in one process and see if managed memory allocation is measured cross AppDomains. If they don't, then you can use this to measure total memory usage in an ASP.NET application.

If you want more specific information there's a diagnostics API for the CLR which you could interface with. There's also plenty of memory profilers out there, but if they'll work within an ASP.NET application I cannot say.

like image 177
John Leidegren Avatar answered Oct 23 '22 05:10

John Leidegren


As an alternative, if you want more detailed information, you can read the performance counters using the System.Diagnostics.PerformanceCounter class. Here are some of the counters that you can plug into:

Request Bytes Out Total

Request Bytes In Total

Request Wait Time

Requests Executing

Requests/Sec

Errors Total

like image 24
Todd Avatar answered Oct 23 '22 03:10

Todd