第四節:監視AppDomain

來源:互聯網
上載者:User

標籤:

宿主應用程式可監視AppDomain消耗的資源。有的宿主根據這種資訊判斷一個AppDomain的記憶體或CPU消耗是否超過了應有的水準,並強制卸載一個AppDomain。

還可以利用監視來比較不同演算法的資源消耗情況,判斷哪種演算法用的資源較少。由於AppDomain監視本身也會產生開銷,所以宿主必須將AppDomain的靜態屬性MonitoringEnabled設為true,從而顯示開啟監視。監視一旦開啟就不能關閉;如果試圖將MonitoringEnabled設為false,會拋出一個ArgumentException。

 

監視開啟後,你的代碼可查詢AppDomain類提供的一下4個唯讀屬性。

  1. MonitoringSurvivedProcessMemorySize  這個Int64靜態屬性返回由當前CLR執行個體控制的所有AppDomain正在使用的位元組數。這個數字只保證上一次記憶體回收時使準確的。
  2. MonitoringTotalAllocatedMemorySize  這個Int64執行個體屬性返回一個特定的AppDomain已指派的位元組數。這個數字只保證上一次記憶體回收時使準確的。
  3. MonitoringSurvivedMemorySize  這個Int64執行個體屬性返回一個特定的AppDomain當前正在使用的位元組數。這個數字只保證上一次記憶體回收時使準確的。
  4. MonitoringTotalProcessorTime 這個TimeSpan執行個體屬性返回一個特定的AppDomain的CPU佔有率。

下面這個示範了如何利用這三個屬性檢查在兩個時間點之間,一個AppDomain中發生的變化:

 

  sealed class AppDomainMonitorDelta : IDisposable    {        private AppDomain m_appDomain;        private TimeSpan m_thisADCpu;        private Int64 m_thisADMemoryInUse;        private Int64 m_thisADMemoryAllocated;        static AppDomainMonitorDelta()        {            //確定以開啟AppDomain監視            AppDomain.MonitoringIsEnabled = true;        }        public AppDomainMonitorDelta(AppDomain ad)        {            m_appDomain = ad ?? AppDomain.CurrentDomain;            //            m_thisADCpu = m_appDomain.MonitoringTotalProcessorTime;            //返回由當前CLR執行個體控制的所有AppDomain正在使用的位元組數            m_thisADMemoryInUse = m_appDomain.MonitoringSurvivedMemorySize;            //返回一個特定的AppDomain已指派的位元組數            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);        }    }

下面示範如何使用AppDomainMonitorDelta類

  private static void AppDomainResourceMonitoring()        {            using (new AppDomainMonitorDelta(null))            {                var list = new List<Object>();                for (Int32 x = 0; x < 1000; x++) list.Add(new Byte[10000]);                for (Int32 x = 0; x < 2000; x++)                    new Byte[1000].GetType();                Int64 stop = Environment.TickCount + 5000;                while (Environment.TickCount < stop) ;            }        }

 

第四節:監視AppDomain

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.