使用windbg排查一个内存溢出的问题

使用windbg排查一个内存溢出的问题发现有一个服务占用大量的内存 奇怪的是服务一开始的时候只占用 100M 左右内存 随着时间推移越来越大 最后导致服务器内存吃紧 这可以算是一种内存泄漏的问题 之所以标题不说是内存泄漏 最后就会知道 并不是因为什么东西没回收导致内存泄漏 于是 dump 了一下进程

大家好,我是讯享网,很高兴认识大家。

发现有一个服务占用大量的内存

image
讯享网

奇怪的是服务一开始的时候只占用100M左右内存,随着时间推移越来越大,最后导致服务器内存吃紧。这可以算是一种内存泄漏的问题,之所以标题不说是内存泄漏,最后就会知道,并不是因为什么东西没回收导致内存泄漏。

于是dump了一下进程,使用windbg打开后先看看托管堆情况:

 

0:000> !eeheap -gc
Number of GC Heaps: 1
generation 0 starts at 0x00000000f2f76a70
generation 1 starts at 0x00000000f2f03400
generation 2 starts at 0x0000000002aa1000
ephemeral segment allocation context: none
         segment            begin         allocated             size
00000000001a0b80 0000064274e17588  0000064274e4d910 0x0000000000036388()
00000000001a0990 00000  00000e58 0x000000000002add0()
0000000000163cd0 00000c3188  00000c38 0x0000000000041ab0()
0000000002aa0000 0000000002aa1000  0000000012a9fd70 0x000000000fffed70()
000000001e 000000001e  000000002e26ec58 0x000000000ffedc58()
000000003b0c0000 000000003b0c1000  000000004b0ac0f0 0x000000000ffeb0f0()
000000007fff0000 000000007fff1000  000000008ffeffb8 0x000000000fffefb8()
000000008fff0000 000000008fff1000  000000009ffea648 0x000000000fff9648()
00000000bfff0000 00000000bfff1000  00000000cffeffd8 0x000000000fffefd8()
00000000cfff0000 00000000cfff1000  00000000dffeffd8 0x000000000fffefd8()
00000000dfff0000 00000000dfff1000  00000000effeffb8 0x000000000fffefb8()
00000000efff0000 00000000efff1000  00000000f31fecf0 0x000000000320dcf0()
Large object heap starts at 0x0000000012aa1000
         segment            begin         allocated             size
0000000012aa0000 0000000012aa1000  000000001aa9b4f0 0x0000000007ffa4f0()
000000002fca0000 000000002fca1000  0000000037c92c88 0x0000000007ff1c88()
000000004b0c0000 000000004b0c1000  00000000530a2368 0x0000000007fe1368()
00000000530c0000 00000000530c1000  000000005b0bb3f0 0x0000000007ffa3f0()
00000000 00000000  00000000b8 0x0000000007fe26b8()
000000009fff0000 000000009fff1000  00000000a7fd37b0 0x0000000007fe27b0()
00000000a7ff0000 00000000a7ff1000  00000000af 0x000000000()
00000000b7ff0000 00000000b7ff1000  00000000bc1a1260 0x00000000041b0260()
000000005b0c0000 000000005b0c1000  000000005f2c1048 0x000000000()
00000000ffff0000 00000000ffff1000  0000000107ff1020 0x000000000()
000000010bff0000 000000010bff1000  00000001101f1048 0x000000000()
0000000113ff0000 0000000113ff1000  000000011bff1020 0x000000000()
000000011fff0000 000000011fff1000  00000001241f1048 0x000000000()
0000000127ff0000 0000000127ff1000  000000012fff1020 0x000000000()
0000000133ff0000 0000000133ff1000  000000013d1f1080 0x000000000()
000000013fff0000 000000013fff1000  0000000 0x000000000()
Total Size        0xf4163e98()
------------------------------
GC Heap Size        0xf4163e98()

 

4G内存占用,然后看看哪些对象最占用内存:

 

0:000> !dumpheap –stat

00000e5b38     5168    System.Int32[]
00000642b78659d0     2294    System.Data.RBTree`1+Node[[System.Int32, mscorlib]][]
00000642b7820d60     2338    System.Data.RBTree`1+Node[[System.Data.DataRow, System.Data]][]
0000000000165b40     4000          Free
00000d4758     4131    System.Object[]
00000642b77fe7c0      System.Data.DataRow
00000aaf8    System.String

 

其中字符串占用1220MB,DataRow占用832MB,object数组占用675MB,就这三位大哥加起来差不多3G了,从什么红黑树结构和DataRow能初步判断,程序中用到了DataTable,866万行数据在内存中,再来算下一个字符串大概也就占用67字节,不是很大,这很可能属于一个DataRow的列数据。随便选一个DataRow开刀:

 

0:000> !dumpheap -mt 00000642b77fe7c0
         Address               MT     Size
0000000002aa2a68 00000642b77fe7c0       96    
0000000002aa9870 00000642b77fe7c0       96    
0000000002aae0c8 00000642b77fe7c0       96    
0000000002aae1b0 00000642b77fe7c0       96    
0000000002aae290 00000642b77fe7c0       96    
0000000002aae380 00000642b77fe7c0       96    
0000000002aae468 00000642b77fe7c0       96    
0000000002aae550 00000642b77fe7c0       96    
0000000002aae648 00000642b77fe7c0       96    
0000000002aae730 00000642b77fe7c0       96    

 

0:000> !do 0000000002aa2a68
Name: System.Data.DataRow
MethodTable: 00000642b77fe7c0
EEClass: 00000642b781e250
Size: 96(0x60) bytes
(C:\WINDOWS\assembly\GAC_64\System.Data\2.0.0.0__b77a5ce089\System.Data.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00000642b77fcfb0  40006ad        8 ...em.Data.DataTable  0 instance 0000000002ab2590 _table
00000642b77ff198  40006ae       10 ...aColumnCollection  0 instance 0000000002aad950 _columns
00000  40006af       30         System.Int32  0 instance            31163 oldRecord
00000  40006b0       34         System.Int32  0 instance            31163 newRecord
00000  40006b1       38         System.Int32  0 instance               -1 tempRecord
00000  40006b2       3c         System.Int32  0 instance            31164 _rowID
00000642b7838d00  40006b3       40         System.Int32  0 instance                0 _action
00000  40006b4       50       System.Boolean  0 instance                0 inChangingEvent
00000  40006b5       51       System.Boolean  0 instance                0 inDeletingEvent
00000  40006b6       52       System.Boolean  0 instance                0 inCascade
00000642b77fe2f0  40006b7       18 ...m.Data.DataColumn  0 instance 0000000000000000 _lastChangedColumn
00000  40006b8       44         System.Int32  0 instance                0 _countColumnChange
00000642b78372f8  40006b9       20 ...em.Data.DataError  0 instance 0000000000000000 error
00000fb0  40006ba       28        System.Object  0 instance 0000000000000000 _element
00000  40006bb       48         System.Int32  0 instance          _rbTreeNodeId
00000  40006bd       4c         System.Int32  0 instance           ObjectID
00000  40006bc      4e8         System.Int32  0   static          _objectTypeCount

 

这是一个DataRow,那么我们来看看这个DataTable的情况:

0:000> !do 0000000002ab2590
Name: System.Data.DataTable
MethodTable: 00000642b77fcfb0
EEClass: 00000642b781d3b0
Size: 512(0x200) bytes
(C:\WINDOWS\assembly\GAC_64\System.Data\2.0.0.0__b77a5ce089\System.Data.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
0000064274e54480  c        8 ...ponentModel.ISite  0 instance 0000000000000000 site
0000064274e4f1d8  d       10 ....EventHandlerList  0 instance 0000000000000000 events
00000fb0  b      3d0        System.Object  0   static 0000000000000000 EventDisposed
0000000000000000         18  System.Data.DataSet  0 instance 0000000000000000 dataSet
00000642b77feb30         20 System.Data.DataView  0 instance 0000000000000000 defaultView
00000        1a0         System.Int32  0 instance          nextRowID
00000642b781ec10         28 ...DataRowCollection  0 instance 0000000002aada90 rowCollection
00000642b77ff198         30 ...aColumnCollection  0 instance 0000000002aad950 columnCollection
00000642b781e9d0         38 ...straintCollection  0 instance 0000000002aada30 constraintCollection
00000        1a4         System.Int32  0 instance                8 elementColumnCount
00000642b         40 ...elationCollection  0 instance 0000000000000000 parentRelationsCollection
00000642b  a       48 ...elationCollection  0 instance 0000000000000000 childRelationsCollection
00000642b77ff080  b       50 ...ata.RecordManager  0 instance 0000000002aad7f0 recordManager
0000000000000000  c       58                       0 instance 0000000002aae060 indexes
0000000000000000  d       60                       0 instance 0000000000000000 shadowIndexes
00000  e      1a8         System.Int32  0 instance                0 shadowCount
00000642b783bce8  f       68 ...ropertyCollection  0 instance 0000000000000000 extendedProperties
00000aaf8         70        System.String  0 instance 0000000002aa4f68 tableName
00000aaf8         78        System.String  0 instance 0000000000000000 tableNamespace

 

想通过!objsize看看这个DataTable的大小,是不是我们找的那个大家伙,可惜等了一个小时也没出来结果,只能放弃那么有没有办法看到行列数呢?

 

0:000> !do 0000000002aad950
Name: System.Data.DataColumnCollection
MethodTable: 00000642b77ff198
EEClass: 00000642b788d078
Size: 96(0x60) bytes
(C:\WINDOWS\assembly\GAC_64\System.Data\2.0.0.0__b77a5ce089\System.Data.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
0000064274e6d8c8  f      220 ...onChangeEventArgs  0   static 0000000000000000 RefreshEventArgs
00000642b77fcfb0          8 ...em.Data.DataTable  0 instance 0000000002ab2590 table
00000e400         10 ...ections.ArrayList  0 instance 0000000002aad9b0 _list
00000         48         System.Int32  0 instance                1 defaultNameIndex
00000d4758         18      System.Object[]  0 instance 0000000000000000 delayedAddRangeColumns
00000d8         20 ...ections.Hashtable  0 instance 0000000002aad9d8 columnFromName
0000064274e67628  a       28 ...hangeEventHandler  0 instance 0000000000000000 onCollectionChangedDelegate
0000064274e67628  b       30 ...hangeEventHandler  0 instance 0000000000000000 onCollectionChangingDelegate
0000064274e67628  c       38 ...hangeEventHandler  0 instance 0000000000000000 onColumnPropertyChangedDelegate
00000  d       54       System.Boolean  0 instance                0 fInClear
00000d4758  e       40      System.Object[]  0 instance 0000000002ab27a8 columnsImplementingIChangeTracking
00000  f       4c         System.Int32  0 instance                0 nColumnsImplementingIChangeTracking
00000         50         System.Int32  0 instance                0 nColumnsImplementingIRevertibleChangeTracking
0:000> !do 0000000002aad9b0
Name: System.Collections.ArrayList
MethodTable: 00000e400
EEClass: 00000b38
Size: 40(0x28) bytes
(C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00000d4758          8      System.Object[]  0 instance 0000000002b72888 _items
00000         18         System.Int32  0 instance                8 _size
00000         1c         System.Int32  0 instance                8 _version
00000fb0         10        System.Object  0 instance 0000000000000000 _syncRoot
00000d4758        360      System.Object[]  0   shared           static emptyArray
                                 >> Domain:Value  0000000000:0000000002aa1e80 <<
0:000> !do 0000000002b72888
Name: System.Object[]
MethodTable: 00000d4758
EEClass: 00000d2f80
Size: 96(0x60) bytes
Array: Rank 1, Number of elements 8, Type CLASS
Element Type: System.Object
Fields:
None

 

这里看到了列数为8,我们选其中一列看看:

 

0:000> !do 0000000002b72640
Name: System.Data.DataColumn
MethodTable: 00000642b77fe2f0
EEClass: 00000642b781e198
Size: 232(0xe8) bytes
(C:\WINDOWS\assembly\GAC_64\System.Data\2.0.0.0__b77a5ce089\System.Data.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
0000064274e54480  c        8 ...ponentModel.ISite  0 instance 0000000000000000 site
0000064274e4f1d8  d       10 ....EventHandlerList  0 instance 0000000000000000 events
00000fb0  b      3d0        System.Object  0   static 0000000000000000 EventDisposed
00000  c       d4       System.Boolean  0 instance                1 allowNull
00000  d       d5       System.Boolean  0 instance                0 autoIncrement
00000d0  e       a0         System.Int64  0 instance 1 autoIncrementStep
00000d0  f       a8         System.Int64  0 instance 0 autoIncrementSeed
00000aaf8         18        System.String  0 instance 0000000000000000 caption
00000aaf8         20        System.String  0 instance 0000000002b62fd0 _columnName
00000a650         28          System.Type  0 instance 0000000002ab6fe0 dataType
00000fb0         30        System.Object  0 instance 0000000002ad8ac0 defaultValue
00000642b7839b30         b8         System.Int32  0 instance                3 _dateTimeMode
00000642b7800ab0         38 ...ta.DataExpression  0 instance 0000000000000000 expression
00000         bc         System.Int32  0 instance               32 maxLength
00000         c0         System.Int32  0 instance                3 _ordinal
00000         d6       System.Boolean  0 instance                0 readOnly
00000642b77ff4a0         40    System.Data.Index  0 instance 0000000000000000 sortIndex
00000642b77fcfb0  a       48 ...em.Data.DataTable  0 instance 0000000002ab2590 table
00000  b       d7       System.Boolean  0 instance                0 unique
00000642b783b5e0  c       c4         System.Int32  0 instance                1 columnMapping
00000  d       c8         System.Int32  0 instance                0 _hashCode
00000  e       cc         System.Int32  0 instance                0 errors
00000  f       d8       System.Boolean  0 instance                0 isSqlType
00000         d9       System.Boolean  0 instance                0 implementsINullable
00000         da       System.Boolean  0 instance                0 implementsIChangeTracking
00000         db       System.Boolean  0 instance                0 implementsIRevertibleChangeTracking
00000         dc       System.Boolean  0 instance                0 implementsIXMLSerializable
00000         dd       System.Boolean  0 instance                1 defaultValueIsNull
0000000000000000         50                       0 instance 0000000000000000 dependentColumns
00000642b783bce8         58 ...ropertyCollection  0 instance 0000000000000000 extendedProperties
0000064274e862f8         60 ...angedEventHandler  0 instance 0000000000000000 onPropertyChangingDelegate
00000642b77f7070         68 ...ommon.DataStorage  0 instance 0000000002b735f8 _storage
00000d0         b0         System.Int64  0 instance 0 autoIncrementCurrent
00000aaf8  a       70        System.String  0 instance 0000000000000000 _columnUri
00000aaf8  b       78        System.String  0 instance 0000000002aa4f68 _columnPrefix
00000aaf8  c       80        System.String  0 instance 0000000000000000 encodedColumnName
00000aaf8  d       88        System.String  0 instance 0000000002aa4f68 description
00000aaf8  e       90        System.String  0 instance 0000000002aa4f68 dttype
00000642b77ffaa0  f       98 ...m.Data.SimpleType  0 instance 0000000002b72728 simpleType
00000         d0         System.Int32  0 instance               35 _objectID
00000        4d0         System.Int32  0   static            10254 _objectTypeCount
0:000> !do 0000000002b62fd0
Name: System.String
MethodTable: 00000aaf8
EEClass: 00000f7d8
Size: 36(0x24) bytes
(C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
String: mapid
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00000          8         System.Int32  0 instance                6 m_arrayLength
00000          c         System.Int32  0 instance                5 m_stringLength
00000d8         10          System.Char  0 instance               6d m_firstChar
00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<

 

看到了,这列叫mapid,有多少行?

 

0:000> !do 0000000002b735f8
Name: System.Data.Common.StringStorage
MethodTable: 00000642b781f218
EEClass: 00000642b788db20
Size: 80(0x50) bytes
(C:\WINDOWS\assembly\GAC_64\System.Data\2.0.0.0__b77a5ce089\System.Data.dll)
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00000642b77fe2f0  4000aab        8 ...m.Data.DataColumn  0 instance 0000000002b72640 Column
00000642b77fcfb0  4000aac       10 ...em.Data.DataTable  0 instance 0000000002ab2590 Table
00000a650  4000aad       18          System.Type  0 instance 0000000002ab6fe0 DataType
00000642b783f6a8  4000aae       38         System.Int32  0 instance               18 StorageTypeCode
00000f8d8  4000aaf       20 ...lections.BitArray  0 instance 0000000000000000 dbNullBits
00000fb0  4000ab0       28        System.Object  0 instance 00000c36e0 DefaultValue
00000fb0  4000ab1       30        System.Object  0 instance 0000000002ad8ac0 NullValue
00000  4000ab2       3c       System.Boolean  0 instance                0 IsCloneable
00000  4000ab3       3d       System.Boolean  0 instance                0 IsCustomDefinedType
00000  4000ab4       3e       System.Boolean  0 instance                1 IsStringType
00000  4000ab5       3f       System.Boolean  0 instance                0 IsValueType
00000d4758  4000aaa      330      System.Object[]  0   static 0000000002b64da0 StorageClassType
00000d4758  4000c26       40      System.Object[]  0 instance 0000000113ff1000 values
0:000> !do 0000000113ff1000
Name: System.Object[]
MethodTable: 00000d4758
EEClass: 00000d2f80
Size: (0x) bytes
Array: Rank 1, Number of elements , Type CLASS
Element Type: System.String
Fields:
None

 

天哪,1600多万行,64位机器指针8字节,光指针占用134MB,别说里面的字符串了。那么基本可以确定这就是罪魁祸首了。现在问题就是想看看里面的数据,那么随便输出几个:

 

0:000> !dumparray -length 10 -details 0000000113ff1000
Name: System.String[]
MethodTable: 00000d4758
EEClass: 00000d2f80
Size: (0x) bytes
Array: Rank 1, Number of elements , Type CLASS
Element Methodtable: 00000aaf8
[0] 0000000002b731c0
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     e51ff7dea1981f7c3a754d6b80   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               65 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[1] 0000000002b73890
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     ec29c4c2e97079b4d33357f1d   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               65 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[2] 0000000002b73978
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     7afb5e8ccefd4b5fb0a251efaec1356a   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               37 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[3] 0000000002b73a58
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     844faa0f6f5e407cae6d9f5dc7cead4f   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               38 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[4] 0000000002b73b38
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     35c966ae4fd445c2b1bae15d6ae4b940   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               33 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[5] 0000000002b73c18
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     cc09aef8fbbbc7d29f98   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               63 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[6] 0000000002b73cf8
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     dd60b243d07f4cffa8e61cccb668fe9e   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               64 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[7] 0000000002b73de0
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     eab4d9ebf4d54e1e9bc020fc   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               65 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[8] 0000000002b73fc8
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     efb54834dc389   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               37 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<
[9] 0000000002b740b0
    Name: System.String
    MethodTable: 00000aaf8
    EEClass: 00000f7d8
    Size: 90(0x5a) bytes
     (C:\WINDOWS\assembly\GAC_64\mscorlib\2.0.0.0__b77a5ce089\mscorlib.dll)
    String:     3c54a40a97ab4d4aa5fdaaeeefa903c1   
    Fields:
                  MT    Field   Offset                 Type VT     Attr            Value Name
    00000          8         System.Int32  0 instance               33 m_arrayLength
    00000          c         System.Int32  0 instance               32 m_stringLength
    00000d8         10          System.Char  0 instance               33 m_firstChar
    00000aaf8         20        System.String  0   shared           static Empty
                                 >> Domain:Value  0000000000:00000c36e0 <<
    00000db830  a       28        System.Char[]  0   shared           static WhitespaceChars
                                 >> Domain:Value  0000000000:0000000002aa14e8 <<

 

好像都是一些md5后的字符串,现在的工作就是到代码中找DataTable了,看到:

 

SqlDataReader sqlDataReader = db.GetReader("select * from typetable");
            ddd.Load(sqlDataReader);

 

这个ddd的DataTable很可疑,因为对照数据库中的表看到这个表有8列:

 

image

并且其中也有一个mapid,那么看看mapid的数据:

image

差不多是这个,那么就是它了,但一看数据量:

image

才2万多,怎么内存里有千万数据??

之后通过探查其它列的数据发现这里面数据都是重复的,说明有重复加载,查找所有和ddd相关的代码:

image

只有3个地方往里面加数据,首先怀疑ddd.Rows.Add,但是后来查看发现只有在数据行不存在的情况下才会加,难道是Load方法?第一Load方法是在程序启动的时候一次性加载的,而第二个是在业务逻辑中做的,查看了一下MSDN傻眼了,由于ddd是静态变量,Load方法每一次都会根据主键检查行是否存在,如果不存在的话作为新行,因此ddd就不断重复加2万多行,导致。。。。

做一个实验:

using (SqlConnection conn = new SqlConnection(""))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("select * from typetable", conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        typetable.Load(reader);
                    }
                }
            }

            MessageBox.Show(typetable.Rows.Count.ToString());

 

 

第一次运行,程序很愉快的输出了:

image

再按一次按钮:

image 

果然这样,那么尝试把第一列设置为主键试试:

if (typetable.Columns.Count > 0)
{
    typetable.PrimaryKey = new DataColumn[] { typetable.Columns[0] };
}

经过试验,问题得到解决。好了,程序中问题找到了,解决办法也有了,接下去就是修改验证的过程了。

感觉这应该不算内存泄漏的问题,而是作者没有意识到DataTable的Load方法在没指明主键的时候作为新行来处理:

 

Load 方法的此版本尝试保留每行中的当前值,使原始值保持不变。(如果要更精细地控制传入数据的行为,请参见 DataTable.Load 方法。)如果现有的行和传入的行包含对应的主键值,将使用其当前行状态值来处理行,否则将该行视为新行。

小讯
上一篇 2025-02-20 20:58
下一篇 2025-02-14 11:13

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/98496.html