Quantcast
Viewing latest article 8
Browse Latest Browse All 9

Measuring AppDomain Resource Utilization

Around a year and half before, I wrote an application which used .Net AppDomains to host background processes and WCF service hosts. One of the motivation was to be able to recycle any AppDomain individually as and when needed. It was on .Net Framework 3.5 and one of the major limitation we faced was inability to measure resource utilized by individual AppDomain. Processor and memory utilization can be monitored for the windows process, but there was no way to know the amount of resources each AppDomain is consuming. If resource consumption at each AppDomain level can be measured, we can have automatic or manual recycle mechanism based on the AppDomain’s health or other criterions like amount of time it is running, number of requests it processed, memory usage, etc. In .Net Framework 4.0 Microsoft added a feature which allows to measure memory and processor time consumption at AppDomain level. I put together a console application, which hosted multiple AppDomains and measured resource consumption for each of them. After looking at running number on console, I thought this need to have better visual representation and I wrote a little WPF application to visualize this data. Following is the resulting application

Image may be NSFW.
Clik here to view.
AppDomainPerfMon

Following is the console application which hosts the AppDomains.

Image may be NSFW.
Clik here to view.
AppDomainPerfMonHoster

Console applicationcation prompts for the number of AppDomains you would like to host. We need to do some processing in the AppDomain, so that we can see some processor and memory utilization. To simulate some work in the AppDomain, a thread is spawned in AppDomain which wakes up after some random amount of time and concatenates random length of string, encrypts and decrypts it. String concatenation results in to large memory use but not much of the CPU usage, so I added encryption and decryption of the string, which flexes the CPU little bit. In the screenshot above you can see a circular gauge indicating processor utilization and two bars indicating total amount of memory allocated since the AppDomain was created and second bar indicating the amount of memory survived after the CLR garbage collection. In the screenshot you can see the survived memory is 0 most of the time because our processing is happening in a method which doesn't hold any object references outside the context of the method.

Monitoring the AppDomain resource utilization is enabled through a static property exposed through AppDomain class.

   1: AppDomain.MonitoringIsEnabled = true;   

Once the monitoring is enables, you can collect the AppDomains resource utilization through following AppDomain instance properties.

   1: healthData.TotalProcessorTime = this.AppDomain.MonitoringTotalProcessorTime;
   2: healthData.TotalAllocatedMemorySize = this.AppDomain.MonitoringTotalAllocatedMemorySize;
   3: healthData.SurvivedMemorySize = this.AppDomain.MonitoringSurvivedMemorySize;

Following is the brief explanation about available properties

1) MonitoringTotalAllocatedMemorySize: All memory(in bytes) allocation that have been made for this AppDomain since it has been created. This will help to know how memory intensive is your processing.

2) MonitoringSurvivedMemorySize: Memory(in bytes) survived after the GC cycle and currently being used by your AppDomain.

3) MonitoringTotalProcessorTime: This is the total time each thread in the process spent executing in that application domain. You need to consider the number of cores on the machine while calculating the processor utilization based on this time. 

4) MonitoringSurvivedProcessMemorySize: Memory(in bytes) survived after the GC cycle and currently being used by all AppDomains in the process. I am not using this property in the sample application as I am measuring it at AppDomain level.

Please note that these properties requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code. So, please run this application or visual studio as an administrator.

As you can see, measuring the resources at AppDomain level is very straight forward. However, I ended up spending some time in creating the infrastructure which can gather the data and publishes to the external applications. I created a health broadcaster WCF service with duplex channel, any interested application can subscribe for the health data by calling ‘SubscribeToHealthBroadcast’ operation. The calling application needs to host the callback contract ‘IHealthBroadcastListener’. The application broadcasts health data to each subscriber at configured interval. I created two simple WPF control to visualize the health data Gauge and Equalizer control. Please note that I wrote these controls only for this application, so they might not be fully mature or reusable.

You can download the complete source code from following link. Please let me know if you have any questions or suggestions.



Image may be NSFW.
Clik here to view.

Viewing latest article 8
Browse Latest Browse All 9

Trending Articles