tags:

views:

211

answers:

2

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.

+2  A: 

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.

John Leidegren
+1  A: 

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

Todd