Perfmon.exe輔助檢查.NET程序內存泄漏

由於工做用C#寫的程序總是內存泄漏,在網上找了找資料後,發現了Windows自帶的性能監視器Perfmon.exe能夠輔助查看.NET程序的運行情況。今天研究了一番,下面的內容就是一些我認爲比較重要須要記下來的東西~~~html

1)在開始菜單中輸入perfmon就能夠找到性能監視器(Perfmon.exe)了git

2)進入性能監視器後,在左側的抽屜菜單中找到「性能監視器」app

能夠發現,Perfmon中已經預設了一個計數器性能

\Processor Information(_Total)\% Processor Timeui

由於這個計數器和咱們的目標沒有太多關係,能夠把它刪除spa

3)添加下面幾個計數器(含義部分採集自Perfmon自身說明).net

①:Process下的Thread Count(注意選擇對象實例-即要監視的具體進程)線程

含義:在此次處理中正在活動的線程數目。指令是在一臺處理器中基本的執行單位,線程是指執行指令的對象。每一個運行處理至少有一個線程。調試

②:Process下的Handle Count(注意選擇對象實例-即要監視的具體進程)code

含義:由這個處理如今打開的句柄總數。這個數字等於這個處理中每一個線程當前打開的句柄的總數。

③:Process下的Private Bytes(注意選擇對象實例-即要監視的具體進程)

含義:Private Bytes 指這個處理不能與其餘處理共享的、已分配的當前字節數。

④:.NET CLR Memory下的 # Bytes in all Heaps

含義:This counter is the sum of four other counters; Gen 0 Heap Size; Gen 1 Heap Size; Gen 2 Heap Size and the Large Object Heap Size. This counter indicates the current memory allocated in bytes on the GC Heaps.

即.NET垃圾回收機制中記錄的託管堆大小

⑤:.NET CLR LocksAndThreads下的 # of current logical Threads(注意選擇對象實例-即要監視的具體進程)

含義:This counter displays the number of current .NET thread objects in the application. A .NET thread object is created either by new System.Threading.Thread or when an unmanaged thread enters the managed environment. This counters maintains the count of both running and stopped threads. This counter is not an average over time; it just displays the last observed value.

注:對於有些計數器(如上面提到的④和⑤),保存的配置與Perfmon.exe中是沒法同時監控的,所以只能同時打開一個監控界面。

4)排查內存泄漏問題的方法

.NET應用程序中的內存分爲堆棧、非託管堆和託管堆

根據上面的Perfmon計數器,檢測內存泄漏的方式以下:

Process/Private Bytes 計數器用於報告系統中專門爲某一進程分配而沒法與其餘進程共享的全部內存。.NET CLR Memory/# Bytes in All Heaps 計數器報告第 0 代、第 1 代、第 2 代和大型對象堆的合計大小。.NET CLR LocksAndThreads/# of current logical Threads 計數器報告 AppDomain 中邏輯線程的數量。若是應用程序的邏輯線程計數出現意想不到的增大,則代表線程堆棧發生泄漏。若是 Private Bytes 增大,而 # Bytes in All Heaps 保持不變,則代表非託管內存發生泄漏。若是上述兩個計數器均有所增長,則代表託管堆中的內存消耗在增加。

記住按期對Perfmon「截圖留念」哦~~~

5)本文參考資料:

1. 調試內存泄漏的應用程序-發現並防止託管代碼中出現內存泄漏 James Kovacs

2. Net資源泄露(內存泄露,GDI泄露,handle 泄露等)的終極解決方案 yuanhuiqiao

附:我用於調試我程序保存的Perfmon設置

<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;" />
<META NAME="GENERATOR" Content="Microsoft System Monitor" />
</HEAD>
<BODY>
<OBJECT ID="DISystemMonitor1" WIDTH="100%" HEIGHT="100%"
CLASSID="CLSID:C4D2D8E0-D1DD-11CE-940F-008029004347">
    <PARAM NAME="_Version" VALUE="458755"/>
    <PARAM NAME="_ExtentX" VALUE="28707"/>
    <PARAM NAME="_ExtentY" VALUE="23363"/>
    <PARAM NAME="DisplayType" VALUE="1"/>
    <PARAM NAME="ReportValueType" VALUE="0"/>
    <PARAM NAME="MaximumScale" VALUE="100"/>
    <PARAM NAME="MinimumScale" VALUE="0"/>
    <PARAM NAME="ShowLegend" VALUE="1"/>
    <PARAM NAME="ShowToolbar" VALUE="1"/>
    <PARAM NAME="ShowScaleLabels" VALUE="1"/>
    <PARAM NAME="ShowHorizontalGrid" VALUE="0"/>
    <PARAM NAME="ShowVerticalGrid" VALUE="0"/>
    <PARAM NAME="ShowValueBar" VALUE="1"/>
    <PARAM NAME="ManualUpdate" VALUE="0"/>
    <PARAM NAME="Highlight" VALUE="0"/>
    <PARAM NAME="ReadOnly" VALUE="0"/>
    <PARAM NAME="MonitorDuplicateInstances" VALUE="1"/>
    <PARAM NAME="UpdateInterval" VALUE="1"/>
    <PARAM NAME="DisplayFilter" VALUE="1"/>
    <PARAM NAME="BackColorCtl" VALUE="-2147483633"/>
    <PARAM NAME="ForeColor" VALUE="-1"/>
    <PARAM NAME="BackColor" VALUE="-1"/>
    <PARAM NAME="GridColor" VALUE="8421504"/>
    <PARAM NAME="TimeBarColor" VALUE="255"/>
    <PARAM NAME="BorderStyle" VALUE="0"/>
    <PARAM NAME="TimeAxisLabels" VALUE="1"/>
    <PARAM NAME="Tooltip" VALUE="1"/>
    <PARAM NAME="NextCounterColor" VALUE="3"/>
    <PARAM NAME="NextCounterWidth" VALUE="2"/>
    <PARAM NAME="NextCounterLineStyle" VALUE="0"/>
    <PARAM NAME="MaximumSamples" VALUE="100"/>
    <PARAM NAME="GraphTitle" VALUE=""/>
    <PARAM NAME="YAxisLabel" VALUE=""/>
    <PARAM NAME="DigitGrouping" VALUE="1"/>
    <PARAM NAME="WrapTimeLine" VALUE="1"/>
    <PARAM NAME="DataSourceType" VALUE="1"/>
    <PARAM NAME="SqlDsnName" VALUE=""/>
    <PARAM NAME="SqlLogSetName" VALUE=""/>
    <PARAM NAME="LogFileCount" VALUE="0"/>
    <PARAM NAME="AmbientFont" VALUE="1"/>
    <PARAM NAME="LegendColumnWidths" VALUE="    8.01131008482564E-02    8.01131008482564E-02    0.200754005655042    9.99057492931197E-02    8.38831291234684E-02    0.170593779453346    0.160226201696513"/>
    <PARAM NAME="LegendSortDirection" VALUE="-2"/>
    <PARAM NAME="LegendSortColumn" VALUE="0"/>
    <PARAM NAME="CounterCount" VALUE="3"/>
    <PARAM NAME="MaximumSamples" VALUE="100"/>
    <PARAM NAME="SampleCount" VALUE="100"/>
    <PARAM NAME="SamplesSoFar" VALUE="111"/>
    <PARAM NAME="SampleIndex" VALUE="11"/>
    <PARAM NAME="StepNumber" VALUE="9"/>
    <PARAM NAME="TimeStamps" VALUE="130512790940000000    130512790950000000    130512790960000000    130512790970000000    130512790980000000    130512790990000000    130512791000000000    130512791010000000    130512791020000000    130512791030000000    130512791040000000    130512790050000000    130512790060000000    130512790070000000    130512790080000000    130512790090000000    130512790100000000    130512790110000000    130512790120000000    130512790130000000    130512790140000000    130512790150000000    130512790160000000    130512790170000000    130512790180000000    130512790190000000    130512790200000000    130512790210000000    130512790220000000    130512790230000000    130512790240000000    130512790250000000    130512790260000000    130512790270000000    130512790280000000    130512790290000000    130512790300000000    130512790310000000    130512790320000000    130512790330000000    130512790340000000    130512790350000000    130512790360000000    130512790370000000    130512790380000000    130512790390000000    130512790400000000    130512790410000000    130512790420000000    130512790430000000    130512790440000000    130512790450000000    130512790460000000    130512790470000000    130512790480000000    130512790490000000    130512790500000000    130512790510000000    130512790520000000    130512790530000000    130512790540000000    130512790550000000    130512790560000000    130512790570000000    130512790580000000    130512790590000000    130512790600000000    130512790610000000    130512790620000000    130512790630000000    130512790640000000    130512790650000000    130512790660000000    130512790670000000    130512790680000000    130512790690000000    130512790700000000    130512790710000000    130512790720000000    130512790730000000    130512790740000000    130512790750000000    130512790760000000    130512790770000000    130512790780000000    130512790790000000    130512790800000000    130512790810000000    130512790820000000    130512790830000000    130512790840000000    130512790850000000    130512790860000000    130512790870000000    130512790880000000    130512790890000000    130512790900000000    130512790910000000    130512790920000000    130512790930000000"/>
    <PARAM NAME="Counter00001.Path" VALUE="\.NET CLR LocksAndThreads(MonitorClient)\# of current logical Threads"/>
    <PARAM NAME="Counter00001.Color" VALUE="255"/>
    <PARAM NAME="Counter00001.Width" VALUE="3"/>
    <PARAM NAME="Counter00001.LineStyle" VALUE="0"/>
    <PARAM NAME="Counter00001.ScaleFactor" VALUE="-1"/>
    <PARAM NAME="Counter00001.Show" VALUE="1"/>
    <PARAM NAME="Counter00001.Selected" VALUE="0"/>
    <PARAM NAME="Counter00001.Minimum" VALUE="215"/>
    <PARAM NAME="Counter00001.Maximum" VALUE="215"/>
    <PARAM NAME="Counter00001.Average" VALUE="215"/>
    <PARAM NAME="Counter00001.StatisticStatus" VALUE="0"/>
    <PARAM NAME="Counter00001.Data" VALUE="215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215    215"/>
    <PARAM NAME="Counter00002.Path" VALUE="\.NET CLR Memory(MonitorClient)\# Bytes in all Heaps"/>
    <PARAM NAME="Counter00002.Color" VALUE="65280"/>
    <PARAM NAME="Counter00002.Width" VALUE="3"/>
    <PARAM NAME="Counter00002.LineStyle" VALUE="0"/>
    <PARAM NAME="Counter00002.ScaleFactor" VALUE="-6"/>
    <PARAM NAME="Counter00002.Show" VALUE="1"/>
    <PARAM NAME="Counter00002.Selected" VALUE="1"/>
    <PARAM NAME="Counter00002.Minimum" VALUE="7144944"/>
    <PARAM NAME="Counter00002.Maximum" VALUE="8554224"/>
    <PARAM NAME="Counter00002.Average" VALUE="7985418.16"/>
    <PARAM NAME="Counter00002.StatisticStatus" VALUE="0"/>
    <PARAM NAME="Counter00002.Data" VALUE="8452740    8452740    8452740    7840708    7840708    7840708    8206060    8206060    8206060    7706116    7706116    7706116    7706116    7725000    7725000    7725000    7725000    7725000    7144944    7144944    8122012    8122012    8448380    8448380    7940264    7940264    7940264    7940264    8554224    8554224    8554224    8554224    8554224    7945892    7945892    7945892    7945892    8345000    8345000    7895744    7895744    7895744    7895744    7895744    7895744    7763948    7763948    7763948    7763948    7763948    7176128    7176128    8059132    8345780    8345780    8345780    7877584    7877584    7877584    7877584    8433608    8433608    8433608    8433608    8433608    7828764    7828764    8116792    8116792    8116792    7688280    7688280    7688280    7688280    7688280    7766128    7766128    7766128    7766128    7173552    7173552    7173552    8058004    8058004    8393116    8393116    7922620    7922620    7922620    7922620    8459028    8459028    8459028    8459028    8459028    7858028    7858028    7858028    8169844    8169844"/>
    <PARAM NAME="Counter00003.Path" VALUE="\Process(MonitorClient)\Private Bytes"/>
    <PARAM NAME="Counter00003.Color" VALUE="16711680"/>
    <PARAM NAME="Counter00003.Width" VALUE="3"/>
    <PARAM NAME="Counter00003.LineStyle" VALUE="0"/>
    <PARAM NAME="Counter00003.ScaleFactor" VALUE="-7"/>
    <PARAM NAME="Counter00003.Show" VALUE="1"/>
    <PARAM NAME="Counter00003.Selected" VALUE="0"/>
    <PARAM NAME="Counter00003.Minimum" VALUE="84230144"/>
    <PARAM NAME="Counter00003.Maximum" VALUE="85676032"/>
    <PARAM NAME="Counter00003.Average" VALUE="84819066.88"/>
    <PARAM NAME="Counter00003.StatisticStatus" VALUE="0"/>
    <PARAM NAME="Counter00003.Data" VALUE="85475328    85377024    85434368    85393408    85385216    85385216    85585920    85516288    85336064    85303296    85303296    85274624    85340160    85368832    85360640    85360640    85299200    85291008    85479424    85471232    85643264    85598208    85676032    85565440    85565440    85565440    85565440    85618688    85368832    85360640    85360640    85360640    85413888    85295104    85295104    85295104    85295104    85389312    85442560    85311488    85311488    85311488    85311488    85241856    85319680    84369408    84369408    84361216    84361216    84418560    84447232    84447232    84402176    84459520    84516864    84447232    84434944    84443136    84443136    84496384    84303872    84295680    84295680    84238336    84291584    84242432    84234240    84234240    84234240    84291584    84230144    84230144    84230144    84234240    84287488    84328448    84320256    84357120    84295680    84451328    84389888    84398080    84398080    84398080    84455424    84377600    84361216    84361216    84361216    84418560    84287488    84275200    84275200    84348928    84295680    84242432    84234240    84234240    84234240    84291584"/>
    <PARAM NAME="Selected: %s" VALUE="\.NET CLR Memory(MonitorClient)\# Bytes in all Heaps"/>
</OBJECT>
</BODY>
</HTML>

END

相關文章
相關標籤/搜索