The host application can monitor the resources consumed by the AppDomain. Some hosts use this information to determine whether an AppDomain's memory or CPU consumption exceeds its proper level and forces an AppDomain to be unloaded.
You can also use monitoring to compare the resource consumption of different algorithms and determine which algorithm uses less resources. Because AppDomain monitoring itself also generates overhead, the host must set the AppDomain's static property monitoringenabled to True to show open monitoring. Monitoring cannot be turned off once it is turned on, and if you attempt to set monitoringenabled to False, a ArgumentException is thrown.
After monitoring is turned on, your code can query the 4 read-only properties provided by the AppDomain class.
- Monitoringsurvivedprocessmemorysize this Int64 static property returns the number of bytes that are being used by all the AppDomain controlled by the current CLR instance. This number only guarantees that the last garbage collection was accurate.
- Monitoringtotalallocatedmemorysize This Int64 instance property returns the number of bytes allocated by a particular AppDomain. This number only guarantees that the last garbage collection was accurate.
- Monitoringsurvivedmemorysize This Int64 instance property returns the number of bytes that a particular AppDomain is currently using. This number only guarantees that the last garbage collection was accurate.
- Monitoringtotalprocessortime This TimeSpan instance property returns the CPU occupancy of a specific AppDomain.
Here's a demonstration of how to use these three properties to check for changes in an AppDomain between two points of time:
Sealed classappdomainmonitordelta:idisposable {PrivateAppDomain M_appdomain; PrivateTimeSpan m_thisadcpu; PrivateInt64 M_thisadmemoryinuse; PrivateInt64 m_thisadmemoryallocated; StaticAppdomainmonitordelta () {//OK to open AppDomain monitoringappdomain.monitoringisenabled =true; } PublicAppdomainmonitordelta (AppDomain AD) {M_appdomain= AD??Appdomain.currentdomain; //M_THISADCPU =M_appdomain.monitoringtotalprocessortime; //returns the number of bytes that are in use by all AppDomain controlled by the current CLR instanceM_thisadmemoryinuse =m_appdomain.monitoringsurvivedmemorysize; //returns the number of bytes allocated by a specific AppDomainm_thisadmemoryallocated =m_appdomain.monitoringtotalallocatedmemorysize; } Public voidDispose () {GC. Collect (); Console.WriteLine ("Friendlyname={0},cpu={1}ms", M_appdomain.friendlyname, (M_appdomain.monitoringtotalprocessortime-m_thisadcpu). TotalMilliseconds); Console.WriteLine ("allocated {0:n0} bytes of which {1:n0} survived GCs", M_appdomain.monitoringtotalallocatedmemorysize-m_thisadmemoryallocated, M_appdomain.monitoringsurvivedmemorysize-m_thisadmemoryinuse); } }
The following shows how to use the Appdomainmonitordelta class
Private Static voidappdomainresourcemonitoring () {using(NewAppdomainmonitordelta (NULL)) { varList =NewList<object>(); for(Int32 x =0; X < +; x + +) list. ADD (Newbyte[10000]); for(Int32 x =0; X < -; X + +) Newbyte[ +]. GetType (); Int64 Stop= Environment.tickcount + the; while(Environment.tickcount <stop); } }
Section Fourth: Monitoring the AppDomain