gc的效果

来源:互联网 发布:还原软件哪个好 编辑:程序博客网 时间:2024/05/22 06:51

工作弄完闲来无事,分析了一下@westonfw 微博上提出的一个关于gc的问题,为了方便调试,我把他的程

序拆分成了3个(代码附后),然后用windbg简单分析了下。

问题贴地址 : http://www.devtext.com/blogs/detail/2671/xmldocument-load-xml-memory-cost-test#jtss-tsina

对每个程序,做如下操作和分析,简要描述如下:
1、启动程序,启动windbg,附加进程,加载sos,这些就不细说了。
2、用!dumpheap -stat 列出heap中的类统计情况,在结果中手工查找XmlDocument
3、确认XmlDocument在heap中存在,那么就去找此类的具体对象,没有就说明已经被gc回收了,结束分析


4、如果找到XmlDocument的具体对象,就去找其具体对象,然后看看这个对象是否还有root,没root了下

次gc就会回收,有root下次gc也不会回收。

具体操作:
1、consoleTest
前面加载sos之类的就不说了,直接上命令
 !dumpheap -stat
total 264133 objects
Statistics:
      MT    Count    TotalSize Class Name
595941fc        1           12 System.Text.DecoderExceptionFallback
595941b8        1           12 System.Text.EncoderExceptionFallback
595921cc        1           12 System.RuntimeTypeHandle
5958fe50        1           12 System.__Filters
5958fe00        1           12 System.Reflection.Missing
5958fd08        1           12 System.RuntimeType+TypeCacheQueue
5958acf4        1           12 System.Collections.Hashtable+ValueCollection
59587da4        1           12 System.Collections.Generic.GenericEqualityComparer`1

[[System.String, mscorlib]]
5957c578        1           12 System.Security.Permissions.FileDialogPermission
5957c53c        1           12 System.Security.PolicyManager
595666fc        1           12 System.Resources.FastResourceComparer
595657d8        1           12 System.Security.Permissions.ReflectionPermission
574ac710        1           12 System.Xml.XmlDownloadManager
574a9f68        1           12 System.Xml.EmptyEnumerator
574a9f28        1           12 System.Xml.XmlImplementation
574a7ce8        1           12 System.Xml.XmlTextReader
574a76ac        1           12 System.Xml.XmlUrlResolver
59593f20        1           16 System.Text.DecoderReplacementFallback
59593ed0        1           16 System.Text.EncoderReplacementFallback
595923cc        1           16 System.Globalization.GlobalizationAssembly
5958d094        1           16 System.Reflection.Cache.InternalCache
5958b158        1           16 System.Security.Permissions.UIPermission
5958a250        1           16 System.Runtime.InteropServices.HandleRef
5957c2f0        1           16 System.Threading.CompressedStack
59567164        1           16 System.Enum+HashEntry
59566980        1           16 System.Resources.ResourceReader

+TypeLimitingDeserializationBinder
595648b8        1           16 System.Double
59594c44        1           20 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
59594bec        1           20 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
59594b60        1           20 System.Text.InternalEncoderBestFitFallback
59590e64        1           20 System.Text.StringBuilder
59589148        1           20 System.Environment+ResourceHelper
595880b8        1           20 System.Security.Permissions.EnvironmentPermission
574aa6b4        1           20 System.Xml.XmlLoader
59594bac        1           24 System.Text.InternalDecoderBestFitFallback
595947fc        1           24 System.IO.TextWriter+SyncTextWriter
5959294c        1           24 System.OperatingSystem
5958b5a8        1           24 System.Collections.Stack
59587e20        1           24 System.String[][]
5957d070        2           24 System.Security.Permissions.StrongNamePublicKeyBlob
59566f0c        2           24 System.OrdinalComparer
5917c324        1           24 Microsoft.Win32.NativeMethods+TokenPrivileges
574aa994        1           24 System.Xml.XmlDeclaration
574a7a64        1           24 System.Xml.XmlTextReaderImpl+XmlContext
574a7928        1           24 System.Xml.NameTable
595946cc        1           28 Microsoft.Win32.Win32Native+InputRecord
59594360        1           28 System.Text.EncoderNLS
59594070        1           28 System.IO.Stream+NullStream
595913f4        1           28 System.SharedStatics
5958da78        1           28 System.Security.SecurityContext
5958d3f0        1           28 System.Reflection.Cache.InternalCacheItem[]
59588f14        1           28 System.CurrentSystemTimeZone
574aa040        1           28 System.Xml.DomNameTable
595942e0        1           32 System.Text.UTF8Encoding+UTF8Encoder
59594254        1           32 System.Threading.IOCompletionCallback
59593338        1           32 System.Security.PermissionTokenFactory
5958d0e0        1           32 System.Reflection.Cache.ClearCacheHandler
59588f6c        1           32 System.Globalization.DaylightTime
5957c9a8        1           32 System.Security.Util.Tokenizer+StringMaker
59568388        2           32 System.DateTime
59566e98        2           32 System.CultureAwareComparer
574a7a1c        1           32 System.Xml.BufferBuilder
59594720        1           36 System.IO.__ConsoleStream
59593154        1           36 System.Security.Permissions.FileIOPermission
59592504        1           36 System.Int64[]
5958da1c        1           36 System.Threading.ExecutionContext
5958ba18        1           36 System.Resources.RuntimeResourceSet
5957ceb0        3           36 System.Security.Policy.AllMembershipCondition
5959328c        2           40 System.Security.PermissionToken
5957ebc0        1           40 System.Int32[][]
595668fc        1           40 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
59566540        1           40 System.IO.BinaryReader
58d216c0        1           40 System.Uri
574a7aac        1           40 System.Xml.XmlNamespaceManager
59bb9650        1           44 System.Text.InternalEncoderBestFitFallbackBuffer
59591814        1           44 System.AppDomainSetup
58d21940        1           44 System.Uri+UriInfo
5958f3a0        1           48 System.Reflection.Module
595682ec        3           48 System.TimeSpan
5958adf4        1           52 System.Resources.ResourceManager
595665f8        1           52 System.Collections.Generic.Dictionary`2[[System.String,

mscorlib],[System.Resources.ResourceLocator, mscorlib]]
595912fc        1           56 System.Threading.Thread
5958d294        1           56 System.LogLevel[]
5958ecf4        3           60 Microsoft.Win32.SafeHandles.SafeFileHandle
595670a8        1           60 System.UInt64[]
595925b4        4           64 System.Int64
59592420        1           64 System.IO.UnmanagedMemoryStream
5958a9fc        2           64 System.Runtime.CompilerServices.RuntimeHelpers+CleanupCode
5958a970        2           64 System.Runtime.CompilerServices.RuntimeHelpers+TryCode
59564c80        2           64 System.Text.UTF8Encoding+UTF8Decoder
59593b50        1           68 System.Globalization.CultureTable
595928d0        3           72 System.Version
5959106c        1           72 System.ExecutionEngineException
59590fdc        1           72 System.StackOverflowException
59590f4c        1           72 System.OutOfMemoryException
59bb9d14        1           76 System.Text.DBCSCodePageEncoding
5958b99c        1           80 System.Resources.ResourceReader
5957d0f0        5           80 System.Security.Policy.ZoneMembershipCondition
5957d000        4           80 System.Security.Policy.PolicyStatement
595652b0        1           80 System.IO.FileStream
59593dcc        3           84 System.Text.UTF8Encoding
59592274        4           96 System.Reflection.Assembly
5958feac        3           96 System.Reflection.MemberFilter
5958aa80        3           96 System.Globalization.CompareInfo
59591508        1          100 System.AppDomain
5958fdc8        1          100 System.Reflection.MetadataArgs+SkipAddresses
5958d1a0        1          108 System.SwitchStructure[]
574a9fd0        3          108 System.Xml.Schema.XmlSchemaInfo
59594510        2          112 System.IO.StreamWriter
59593be4        6          120 System.Globalization.CultureTableItem
59592c08       10          120 System.Security.Permissions.SecurityPermission
5958cd88        2          120 System.Collections.Hashtable+SyncHashtable
59577a38        4          128 System.Collections.ArrayList+SyncArrayList
5958b544        7          140 Microsoft.Win32.SafeHandles.SafeRegistryHandle
574a7b3c        1          140 System.Xml.XmlNamespaceManager+NamespaceDeclaration[]
595929ec        4          144 System.Security.PermissionSet
595910fc        2          144 System.Threading.ThreadAbortException
59566acc        1          152 System.Collections.Generic.Dictionary`2+Entry[[System.String,

mscorlib],[System.Resources.ResourceLocator, mscorlib]][]
5957ee10        3          156 System.Security.Policy.PolicyLevel
574a89e0        1          156 System.Xml.XmlDocument
58d06b00        1          160 System.Diagnostics.Process
5958b344        7          224 Microsoft.Win32.RegistryKey
59170e84       20          240 System.Diagnostics.NtProcessManager+ValueId
595932ec        7          252 System.Security.Util.TokenBasedSet
59593c8c        2          256 System.Globalization.NumberFormatInfo
58d218e0       14          280 System.UriParser+BuiltInUriParser
59590944       27          324 System.Object
574a7708        1          344 System.Xml.XmlTextReaderImpl
5957cbe8       10          360 System.Security.Policy.UnionCodeGroup
574aa084       10          360 System.Xml.XmlName
59593b00        8          384 System.Globalization.CultureTableRecord
59593924        7          476 System.Globalization.CultureInfo
5957ee68       12          576 System.Security.NamedPermissionSet
574a7980       30          600 System.Xml.NameTable+Entry
574a79c4        8          672 System.Xml.XmlTextReaderImpl+NodeData
59592ec4       18          712 System.Int32[]
59592f74       84         1008 System.Int32
5959337c       21         1176 System.Collections.Hashtable
59591ea8       62         1240 System.RuntimeType
5957cb0c       65         1560 System.Security.Policy.StrongNameMembershipCondition
5957c8b8      232         6496 System.Security.SecurityElement
591899e0       69         7452 System.Diagnostics.ProcessInfo
59593478       24         7824 System.Collections.Hashtable+bucket[]
59189c44       69        13248 System.Diagnostics.NtProcessInfoHelper

+SystemProcessInformation
59591958       30        21104 System.Char[]
59189a1c     1071        38556 System.Diagnostics.ThreadInfo
59189c80     1071        77112 System.Diagnostics.NtProcessInfoHelper+SystemThreadInformation
59593798        9       140600 System.Byte[]
574aa59c    26674       426784 System.Xml.XmlAttributeCollection
003f1338      518       478980      Free
574ac760    26674       533480 System.Xml.XmlAttribute
59592d58    27021       648504 System.Collections.ArrayList
595644f8    27195       910724 System.Object[]
574a9d74    38724      1084272 System.Xml.XmlElement
574aa278    56666      1133320 System.Xml.XmlText
59590d28    57484      6751000 System.String
Total 264133 objects

看了下,找到XmlDocument这行了,574a89e0        1          156 System.Xml.XmlDocument
原来还有一个XmlDocument对象,好,那看看吧。
!dumpheap -mt 574a89e0
 Address       MT     Size
0227cfe0 574a89e0      156    
total 1 objects
Statistics:
      MT    Count    TotalSize Class Name
574a89e0        1          156 System.Xml.XmlDocument
Total 1 objects
看来确实还有1个对象在heap中,进一步分析下,它的root情况:
!gcroot 0227cfe0
Note: Roots found on stacks may be false positives. Run "!help gcroot" for
more info.
Scan Thread 0 OSTHread 14e4
Scan Thread 2 OSTHread 1754
好了,发现了吧,没有root了,什么意思?下次gc发生的话,该对象就会被回收了,明白了吧。

现在看看ConsoleTest2的情况
0:007> !dumpheap -stat
total 4881 objects
Statistics:
      MT    Count    TotalSize Class Name
595941fc        1           12 System.Text.DecoderExceptionFallback
595941b8        1           12 System.Text.EncoderExceptionFallback
595921cc        1           12 System.RuntimeTypeHandle
5958fe50        1           12 System.__Filters
5958fe00        1           12 System.Reflection.Missing
5958fd08        1           12 System.RuntimeType+TypeCacheQueue
5958acf4        1           12 System.Collections.Hashtable+ValueCollection
59587da4        1           12 System.Collections.Generic.GenericEqualityComparer`1

[[System.String, mscorlib]]
5957c578        1           12 System.Security.Permissions.FileDialogPermission
5957c53c        1           12 System.Security.PolicyManager
595666fc        1           12 System.Resources.FastResourceComparer
595657d8        1           12 System.Security.Permissions.ReflectionPermission
574ac710        1           12 System.Xml.XmlDownloadManager
574a9f68        1           12 System.Xml.EmptyEnumerator
59593f20        1           16 System.Text.DecoderReplacementFallback
59593ed0        1           16 System.Text.EncoderReplacementFallback
595923cc        1           16 System.Globalization.GlobalizationAssembly
5958b158        1           16 System.Security.Permissions.UIPermission
5958a250        1           16 System.Runtime.InteropServices.HandleRef
59567164        1           16 System.Enum+HashEntry
59566980        1           16 System.Resources.ResourceReader

+TypeLimitingDeserializationBinder
595648b8        1           16 System.Double
59594c44        1           20 Microsoft.Win32.SafeHandles.SafeFileMappingHandle
59594bec        1           20 Microsoft.Win32.SafeHandles.SafeViewOfFileHandle
59594b60        1           20 System.Text.InternalEncoderBestFitFallback
59589148        1           20 System.Environment+ResourceHelper
595880b8        1           20 System.Security.Permissions.EnvironmentPermission
59594bac        1           24 System.Text.InternalDecoderBestFitFallback
595947fc        1           24 System.IO.TextWriter+SyncTextWriter
5959294c        1           24 System.OperatingSystem
5958b5a8        1           24 System.Collections.Stack
59587e20        1           24 System.String[][]
5957d070        2           24 System.Security.Permissions.StrongNamePublicKeyBlob
59566f0c        2           24 System.OrdinalComparer
5917c324        1           24 Microsoft.Win32.NativeMethods+TokenPrivileges
595946cc        1           28 Microsoft.Win32.Win32Native+InputRecord
59594360        1           28 System.Text.EncoderNLS
59594070        1           28 System.IO.Stream+NullStream
595913f4        1           28 System.SharedStatics
5958da78        1           28 System.Security.SecurityContext
59588f14        1           28 System.CurrentSystemTimeZone
595942e0        1           32 System.Text.UTF8Encoding+UTF8Encoder
59594254        1           32 System.Threading.IOCompletionCallback
59593338        1           32 System.Security.PermissionTokenFactory
59588f6c        1           32 System.Globalization.DaylightTime
5957c9a8        1           32 System.Security.Util.Tokenizer+StringMaker
59568388        2           32 System.DateTime
59566e98        2           32 System.CultureAwareComparer
59564c80        1           32 System.Text.UTF8Encoding+UTF8Decoder
59594720        1           36 System.IO.__ConsoleStream
59593154        1           36 System.Security.Permissions.FileIOPermission
59592504        1           36 System.Int64[]
5958da1c        1           36 System.Threading.ExecutionContext
5958ba18        1           36 System.Resources.RuntimeResourceSet
5957ceb0        3           36 System.Security.Policy.AllMembershipCondition
5959328c        2           40 System.Security.PermissionToken
5958ecf4        2           40 Microsoft.Win32.SafeHandles.SafeFileHandle
5957ebc0        1           40 System.Int32[][]
595668fc        1           40 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
59566540        1           40 System.IO.BinaryReader
59bb9650        1           44 System.Text.InternalEncoderBestFitFallbackBuffer
59591814        1           44 System.AppDomainSetup
5958f3a0        1           48 System.Reflection.Module
595682ec        3           48 System.TimeSpan
5958adf4        1           52 System.Resources.ResourceManager
595665f8        1           52 System.Collections.Generic.Dictionary`2[[System.String,

mscorlib],[System.Resources.ResourceLocator, mscorlib]]
59593dcc        2           56 System.Text.UTF8Encoding
595912fc        1           56 System.Threading.Thread
5958d294        1           56 System.LogLevel[]
595670a8        1           60 System.UInt64[]
595925b4        4           64 System.Int64
59592420        1           64 System.IO.UnmanagedMemoryStream
5958a9fc        2           64 System.Runtime.CompilerServices.RuntimeHelpers+CleanupCode
5958a970        2           64 System.Runtime.CompilerServices.RuntimeHelpers+TryCode
59593b50        1           68 System.Globalization.CultureTable
595928d0        3           72 System.Version
5959106c        1           72 System.ExecutionEngineException
59590fdc        1           72 System.StackOverflowException
59590f4c        1           72 System.OutOfMemoryException
59bb9d14        1           76 System.Text.DBCSCodePageEncoding
5958b99c        1           80 System.Resources.ResourceReader
5957d0f0        5           80 System.Security.Policy.ZoneMembershipCondition
5957d000        4           80 System.Security.Policy.PolicyStatement
574a79c4        1           84 System.Xml.XmlTextReaderImpl+NodeData
59592274        4           96 System.Reflection.Assembly
5958feac        3           96 System.Reflection.MemberFilter
5958aa80        3           96 System.Globalization.CompareInfo
59591508        1          100 System.AppDomain
5958fdc8        1          100 System.Reflection.MetadataArgs+SkipAddresses
5958d1a0        1          108 System.SwitchStructure[]
574a9fd0        3          108 System.Xml.Schema.XmlSchemaInfo
59594510        2          112 System.IO.StreamWriter
59593be4        6          120 System.Globalization.CultureTableItem
59592c08       10          120 System.Security.Permissions.SecurityPermission
5958cd88        2          120 System.Collections.Hashtable+SyncHashtable
59577a38        4          128 System.Collections.ArrayList+SyncArrayList
5958b544        7          140 Microsoft.Win32.SafeHandles.SafeRegistryHandle
595929ec        4          144 System.Security.PermissionSet
595910fc        2          144 System.Threading.ThreadAbortException
59566acc        1          152 System.Collections.Generic.Dictionary`2+Entry[[System.String,

mscorlib],[System.Resources.ResourceLocator, mscorlib]][]
5957ee10        3          156 System.Security.Policy.PolicyLevel
00371340       10          156      Free
58d06b00        1          160 System.Diagnostics.Process
5958b344        7          224 Microsoft.Win32.RegistryKey
59170e84       20          240 System.Diagnostics.NtProcessManager+ValueId
595932ec        7          252 System.Security.Util.TokenBasedSet
59593c8c        2          256 System.Globalization.NumberFormatInfo
58d218e0       14          280 System.UriParser+BuiltInUriParser
59590944       26          312 System.Object
5957cbe8       10          360 System.Security.Policy.UnionCodeGroup
59593b00        8          384 System.Globalization.CultureTableRecord
59593924        7          476 System.Globalization.CultureInfo
5957ee68       12          576 System.Security.NamedPermissionSet
59592ec4       18          712 System.Int32[]
59592f74       89         1068 System.Int32
5959337c       21         1176 System.Collections.Hashtable
59591ea8       62         1240 System.RuntimeType
5957cb0c       65         1560 System.Security.Policy.StrongNameMembershipCondition
59591958       29         4704 System.Char[]
5957c8b8      232         6496 System.Security.SecurityElement
59593478       24         7824 System.Collections.Hashtable+bucket[]
591899e0       74         7992 System.Diagnostics.ProcessInfo
59592d58      352         8448 System.Collections.ArrayList
59189c44       74        14208 System.Diagnostics.NtProcessInfoHelper

+SystemProcessInformation
59189a1c     1103        39708 System.Diagnostics.ThreadInfo
59590d28      808        50400 System.String
595644f8      529        57320 System.Object[]
59189c80     1103        79416 System.Diagnostics.NtProcessInfoHelper+SystemThreadInformation
59593798        8       132396 System.Byte[]
Total 4881 objects

找不到XmlDocument啊,明显heap中已经无XmlDocument对象啊。而且Total 4881 objects 对比第一个,

Total 264133 objects,明显很多对象都被回收了,发现GC.Collect()的威力了吗?
看了下ConsoleTest3的情况,跟这个基本一致,就不贴结果了。

我看了一下输出,发现和@westonfw 贴出来的结果第一个差距比较大,他第一个貌似是已经回收后的结果

,不理解。

总结一下:

1、doc = null导致 分配的XmlDocument变为无根之水,这是gc回收的前提。

2、gc的时机本身是不确定的,但是可调用gc.collect()强制触发gc,回收就会把无根的对象都回收掉。(有析构函数的除外,那是另一个故事)

3、不建议主动调用gc.collect(),根据msdn的文章,这会影响后续gc触发时机的判断。这个主要是在一些特殊情况:比如已知有大量可回收对象需要回收,不愿意在后续某个确定时机发生,导致可能影响后续某个需要响应很快的业务。(gc会先挂起excute enging的)

 

by  @已被盗号多年

原创粉丝点击