内存泄露
症状:
在Windows Server 2008 标准版、2008 R2 标准版、 Windows Server 2012 标准版中, 程序运行一段时间以后,使用内存暴涨,如不重启,内存会被程序占用完。 但是在Windows Server 2003 标准版中内存使用是正常的(30M左右)
使用Adplus hang模式,抓出了dump文件, 大小约4.3GB.
在WinDbg中,
!EEHeap -stat 的结果如下:
--------------------------------------
Total LoaderHeap size: 0x4b608000(1264615424)bytes
=======================================
Number of GC Heaps: 1
generation 0 starts at 0x0000000010ed0ce0
generation 1 starts at 0x0000000010e8ec78
generation 2 starts at 0x0000000002671000
ephemeral segment allocation context: none
segment begin allocated size
0000000002670000 0000000002671000 0000000010fbdf08 0x000000000e94cf08(244633352)
Large object heap starts at 0x0000000012671000
segment begin allocated size
0000000012670000 0000000012671000 00000000199ba1c0 0x00000000073491c0(120885696)
Total Size 0x15c960c8(365519048)
------------------------------
GC Heap Size 0x15c960c8(365519048)
可以看到Heap才365M, 但是Dump File 却有4.3GB.
另外就是Loader Heap有1.2GB.
加上-loader参数以后,发现有几千个module , 如下
Module 000007ff4ff70b28: Size: 0x0(0)bytes.
Module 000007ff4ff724e8: Size: 0x0(0)bytes.
Module 000007ff4ff73ea8: Size: 0x0(0)bytes.
Module 000007ff4ff75868: Size: 0x0(0)bytes.
Module 000007ff4ff77228: Size: 0x0(0)bytes.
Module 000007ff4ff78be8: Size: 0x0(0)bytes.
Module 000007ff4ff7a5a8: Size: 0x0(0)bytes.
Module 000007ff4ff7bf68: Size: 0x0(0)bytes.
Module 000007ff4ff7d928: Size: 0x0(0)bytes.
Module 000007ff4ff7f2e8: Size: 0x0(0)bytes.
Module 000007ff4ffb0d30: Size: 0x0(0)bytes.
Module 000007ff4ffb26f0: Size: 0x0(0)bytes.
Module 000007ff4ffb40b0: Size: 0x0(0)bytes.
Module 000007ff4ffb5a70: Size: 0x0(0)bytes.
Module 000007ff4ffb7430: Size: 0x0(0)bytes.
Module 000007ff4ffb8df0: Size: 0x0(0)bytes.
Module 000007ff4ffba7b0: Size: 0x0(0)bytes.
Module 000007ff4ffbc170: Size: 0x0(0)bytes.
Module 000007ff4ffbdb30: Size: 0x0(0)bytes.
Module 000007ff4ffbf4f0: Size: 0x0(0)bytes.
Module 000007ff4fff0f38: Size: 0x0(0)bytes.
Module 000007ff4fff28f8: Size: 0x0(0)bytes.
Module 000007ff4fff42b8: Size: 0x0(0)bytes.
........
随机检查过很多个module ,发现内容其实是一样的,如下
0:019> !Dumpmodule -mt 000007ff504f80e0
Name: 3jrktwys, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Attributes: PEFile
Assembly: 000000015915cba0
LoaderHeap: 0000000000000000
TypeDefToMethodTableMap: 000007ff5050aa78
TypeRefToMethodTableMap: 000007ff5050aae8
MethodDefToDescMap: 000007ff5050ac68
FieldDefToDescMap: 000007ff5050af08
MemberRefToDescMap: 000007ff5050b378
FileReferencesMap: 000007ff5050b550
AssemblyReferencesMap: 000007ff5050b558
MetaData start address: 000000015a262264 (9088 bytes)
Types defined in this module
MT TypeDef Name
------------------------------------------------------------------------------
000007ff504f8808 0x02000002 WMINET_Converter
000007ff504f8918 0x02000003 IWmiConverter
000007ff504f8ae0 0x02000004 ConvertClass_0
000007ff504f8ce8 0x02000005 ConvertClass_1
000007ff504f8ef0 0x02000006 ConvertClass_2
000007ff504f9138 0x02000007 ConvertClass_3
000007ff504f9340 0x02000008 ConvertClass_4
000007ff504f95c8 0x02000009 ConvertClass_5
000007ff504f97f0 0x0200000a ConvertClass_6
000007ff504f99f8 0x0200000b ConvertClass_7
Types referenced in this module
MT TypeRef Name
------------------------------------------------------------------------------
000007fef8c97590 0x01000001 System.Object
000007fef8c9f5f8 0x01000002 System.Collections.Hashtable
000007fef8c925a8 0x01000005 System.Guid
000007ff00245440 0x0100000c Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceEvent
000007fef8c99978 0x0100000d System.Type
000007ff00248e48 0x0100000f Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionFailedEvent
000007ff00249580 0x01000010 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataTransactionFailedEvent
000007ff00249748 0x01000011 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceFailureEvent
000007ff00245590 0x01000012 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataConnectionOpenedEvent
000007ff00249d18 0x01000013 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataServiceConfigFailureEvent
000007ff00249e90 0x01000014 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataCommandFailedEvent
000007ff0024a468 0x01000015 Microsoft.Practices.EnterpriseLibrary.Data.Instrumentation.DataTransactionRolledBackEvent
我的代码中并没有类似
WMINET_Converter
IWmiConverter
之类的定义,但是有引用微软的企业库. 从微软公布的.net sourcecode中查到,这几个class貌似属于
System.Management.Instrumentation名称空间下的定义。
不知道是哪里出了问题,导致出现占用大量的内存?
(补充:如果将Windows 2008打到最新的补丁, 这个内存泄露的问题也可以解决。 但因为补丁太多,具体不知道是哪个解决的这个问题, 有高人能提点下么?)