因为工作用C#写的程序老是内存泄漏,在网上找了找资料后,发现了Windows自带的性能监视器Perfmon.exe可以辅助查看.NET程序的运行状况。今天研究了一番,下面的内容就是一些我认为比较重要需要记下来的东西~~~
1)在开始菜单中输入perfmon就可以找到性能监视器(Perfmon.exe)了
2)进入性能监视器后,在左侧的抽屉菜单中找到“性能监视器”
可以发现,Perfmon中已经预设了一个计数器
\Processor Information(_Total)\% Processor Time
因为这个计数器和我们的目标没有太多关系,可以把它删除
3)添加下面几个计数器(含义部分采集自Perfmon自身说明)
①:Process下的Thread Count(注意选择对象实例-即要监视的具体进程)
含义:在这次处理中正在活动的线程数目。指令是在一台处理器中基本的执行单位,线程是指执行指令的对象。每个运行处理至少有一个线程。
②:Process下的Handle Count(注意选择对象实例-即要监视的具体进程)
含义:由这个处理现在打开的句柄总数。这个数字等于这个处理中每个线程当前打开的句柄的总数。
③: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
来源:oschina
链接:https://my.oschina.net/u/1425762/blog/296580