![]() CATEGORIES: BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism |
Nbsp; AppDomain MonitoringA host application can monitor the resources that an AppDomain consumes. Some hosts will use this information to decide when to forcibly unload an AppDomain should its memory or CPU con- sumption rise above what the host considers reasonable. Monitoring can also be used to compare the resource consumption of different algorithms to determine which uses fewer resources. Because AppDomain monitoring incurs additional overhead, hosts must explicitly turn the monitoring on by setting AppDomain’s static MonitoringEnabled property to true. This turns on monitoring for all AppDomains. After monitoring is turned on, it cannot be turned off; attempting to set the MonitoringEnabled property to false causes an ArgumentException to be thrown. After monitoring is turned on, your code can query the following four read-only properties of- fered by the AppDomain class: ■ MonitoringSurvivedProcessMemorySizeThis static Int64 property returns the number of bytes that are currently in use by all AppDomains controlled by the current CLR instance. The number is accurate as of the last garbage collection. ■ MonitoringTotalAllocatedMemorySizeThis instance Int64 property returns the num- ber of bytes that have been allocated by a specific AppDomain. The number is accurate as of the last garbage collection. ■ MonitoringSurvivedMemorySizeThis instance Int64 property returns the number of bytes that are currently in use by a specific AppDomain. The number is accurate as of the last garbage collection. ■ MonitoringTotalProcessorTimeThis instance TimeSpan property returns the amount of CPU usage incurred by a specific AppDomain.
The following class shows how to use three of these properties to see what has changed within an AppDomain between two points in time.
private sealed class AppDomainMonitorDelta : IDisposable { private AppDomain m_appDomain; private TimeSpan m_thisADCpu; private Int64 m_thisADMemoryInUse; private Int64 m_thisADMemoryAllocated;
static AppDomainMonitorDelta() { // Make sure that AppDomain monitoring is turned on AppDomain.MonitoringIsEnabled = true; }
public AppDomainMonitorDelta(AppDomain ad) { m_appDomain = ad ?? AppDomain.CurrentDomain; m_thisADCpu = m_appDomain.MonitoringTotalProcessorTime; m_thisADMemoryInUse = m_appDomain.MonitoringSurvivedMemorySize; m_thisADMemoryAllocated = m_appDomain.MonitoringTotalAllocatedMemorySize; }
public void Dispose() { 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 code shows how to use the AppDomainMonitorDelta class.
private static void AppDomainResourceMonitoring() { using (new AppDomainMonitorDelta(null)) { // Allocate about 10 million bytes that will survive collections var list = new List<Object>(); for (Int32 x = 0; x < 1000; x++) list.Add(new Byte[10000]);
// Allocate about 20 million bytes that will NOT survive collections for (Int32 x = 0; x < 2000; x++) new Byte[10000].GetType();
// Spin the CPU for about 5 seconds Int64 stop = Environment.TickCount + 5000; while (Environment.TickCount < stop) ; } }
When I execute this code, I get the following output.
FriendlyName=03Ch221AppDomains.exe, CPU=5031.25ms Allocated 30,159,496 bytes of which 10,085,080 survived GCs
Date: 2016-03-03; view: 728
|