1. 程式人生 > 其它 >記憶體暴增排查分析

記憶體暴增排查分析

一次偶然間,發現測試環境iis站點記憶體突然間暴增,平常都是300M,這次一下子暴增到8g 於是就開始了接下來的分析

發現Dictionary居然有1.78g懵逼

windbg 分析

1. 看看當前程序的 commit 記憶體有多大

0:000> !address -summary                          
Mapping file section regions...
Mapping module regions...
Mapping PEB regions...
Mapping TEB and stack regions...
Mapping heap regions...
Mapping page heap regions...
Mapping other regions...
Mapping stack trace database regions...
Mapping activation context regions...

--- Usage Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal Free 402 7df9`8ed48000 ( 125.975 TB) 98.42% <unknown> 1239 206`619ca000 ( 2.025 TB) 99.99% 1.58% Image
1833 0`0b792000 ( 183.570 MB) 0.01% 0.00% Heap 36 0`02cdf000 ( 44.871 MB) 0.00% 0.00% Stack 111 0`01240000 ( 18.250 MB) 0.00% 0.00% Other 13 0`001e2000 ( 1.883 MB) 0.00% 0.00% TEB
37 0`0004a000 ( 296.000 kB) 0.00% 0.00% PEB 1 0`00001000 ( 4.000 kB) 0.00% 0.00% --- Type Summary (for busy) ------ RgnCount ----------- Total Size -------- %ofBusy %ofTotal MEM_MAPPED 326 200`00c57000 ( 2.000 TB) 98.76% 1.56% MEM_PRIVATE 1111 6`64ebf000 ( 25.577 GB) 1.23% 0.02% MEM_IMAGE 1833 0`0b792000 ( 183.570 MB) 0.01% 0.00% --- State Summary ---------------- RgnCount ----------- Total Size -------- %ofBusy %ofTotal MEM_FREE 402 7df9`8ed48000 ( 125.975 TB) 98.42% MEM_RESERVE 431 205`5900d000 ( 2.021 TB) 99.79% 1.58% MEM_COMMIT 2839 1`1829b000 ( 4.378 GB) 0.21% 0.00% --- Protect Summary (for commit) - RgnCount ----------- Total Size -------- %ofBusy %ofTotal PAGE_READWRITE 978 1`08d1c000 ( 4.138 GB) 0.20% 0.00% PAGE_EXECUTE_READ 308 0`07428000 ( 116.156 MB) 0.01% 0.00% PAGE_READONLY 1061 0`0487b000 ( 72.480 MB) 0.00% 0.00% PAGE_NOACCESS 139 0`02909000 ( 41.035 MB) 0.00% 0.00% PAGE_EXECUTE_READWRITE 41 0`00a6f000 ( 10.434 MB) 0.00% 0.00% PAGE_WRITECOPY 273 0`004cf000 ( 4.809 MB) 0.00% 0.00% PAGE_READWRITE|PAGE_GUARD 37 0`00071000 ( 452.000 kB) 0.00% 0.00% PAGE_EXECUTE_WRITECOPY 2 0`00024000 ( 144.000 kB) 0.00% 0.00% --- Largest Region by Usage ----------- Base Address -------- Region Size ---------- Free 18b`42fc0000 7c69`2df10000 ( 124.411 TB) <unknown> 7dfb`9b527000 1f9`b0c13000 ( 1.975 TB) Image 18a`10662000 0`00d7d000 ( 13.488 MB) Heap 18a`13631000 0`00f5e000 ( 15.367 MB) Stack 44`0acd0000 0`0007b000 ( 492.000 kB) Other 185`c4a60000 0`00181000 ( 1.504 MB) TEB 44`0ae08000 0`00002000 ( 8.000 kB) PEB 44`0afac000 0`00001000 ( 4.000 kB)

可以看出大概佔了4.378G,接下來再看看託管堆記憶體。

0:000> !eeheap -gc
Number of GC Heaps: 4
------------------------------
Heap 0 (00000185C55B4F90)
generation 0 starts at 0x00000185C6C45838
generation 1 starts at 0x00000185C6C3DC88
generation 2 starts at 0x00000185C5CB1000
ephemeral segment allocation context: none
         segment             begin         allocated              size
00000185C5CB0000  00000185C5CB1000  00000185C6EB1850  0x1200850(18876496)
Large object heap starts at 0x00000189C5CB1000
         segment             begin         allocated              size
00000189C5CB0000  00000189C5CB1000  00000189D435D6F0  0xe6ac6f0(241878768)
0000018580000000  0000018580001000  00000185ADC5CD08  0x2dc5bd08(767933704)
0000018AE2FC0000  0000018AE2FC1000  0000018B3E878BC8  0x5b8b7bc8(1535867848)
Heap Size:       Size: 0x98dc0810 (2564556816) bytes.
------------------------------
Heap 1 (00000185C56835E0)
generation 0 starts at 0x00000186C6C263D8
generation 1 starts at 0x00000186C6C18578
generation 2 starts at 0x00000186C5CB1000
ephemeral segment allocation context: none
         segment             begin         allocated              size
00000186C5CB0000  00000186C5CB1000  00000186C7293328  0x15e2328(22946600)
Large object heap starts at 0x00000189D5CB1000
         segment             begin         allocated              size
00000189D5CB0000  00000189D5CB1000  00000189D5F53350  0x2a2350(2761552)
Heap Size:       Size: 0x1884678 (25708152) bytes.
------------------------------
Heap 2 (00000185C5576960)
generation 0 starts at 0x00000187C6C3B2A0
generation 1 starts at 0x00000187C6C334E0
generation 2 starts at 0x00000187C5CB1000
ephemeral segment allocation context: none
         segment             begin         allocated              size
00000187C5CB0000  00000187C5CB1000  00000187C768E0D8  0x19dd0d8(27119832)
Large object heap starts at 0x00000189E5CB1000
         segment             begin         allocated              size
00000189E5CB0000  00000189E5CB1000  00000189F326E728  0xd5bd728(224122664)
Heap Size:       Size: 0xef9a800 (251242496) bytes.
------------------------------
Heap 3 (00000185C557E9B0)
generation 0 starts at 0x00000188C6F71D30
generation 1 starts at 0x00000188C6F4F758
generation 2 starts at 0x00000188C5CB1000
ephemeral segment allocation context: none
         segment             begin         allocated              size
00000188C5CB0000  00000188C5CB1000  00000188C7543728  0x1892728(25765672)
Large object heap starts at 0x00000189F5CB1000
         segment             begin         allocated              size
00000189F5CB0000  00000189F5CB1000  00000189F5D310C0  0x800c0(524480)
0000018AB2FC0000  0000018AB2FC1000  0000018AE0C1CD08  0x2dc5bd08(767933704)
0000018A14590000  0000018A14591000  0000018A2B3BEF20  0x16e2df20(383967008)
Heap Size:       Size: 0x4639c410 (1178190864) bytes.
------------------------------
GC Heap Size:    Size: 0xef97ba98 (4019698328) bytes.

檢視託管堆

0:000> !dumpheap -stat 0000018AE2FC1000 0000018B3E878BC8
Statistics:
              MT    Count    TotalSize Class Name
00000185c56d3570        1           30      Free
00007ffb0720ee48        1   1535867816 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]][]
Total 2 objects


0:000> !dumpheap -mt 00007ffb0720ee48 0000018AE2FC1000 0000018B3E878BC8
         Address               MT     Size
0000018ae2fc1020 00007ffb0720ee48 1535867816     

Statistics:
              MT    Count    TotalSize Class Name
00007ffb0720ee48        1   1535867816 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]][]
Total 1 objects

0:000> !DumpHeap /d -mt 00007ffb0720ee48
         Address               MT     Size
00000185c6a56ac8 00007ffb0720ee48      616     
0000018580001020 00007ffb0720ee48 767933672     
0000018ae2fc1020 00007ffb0720ee48 1535867816     
00000188c6da9e30 00007ffb0720ee48      296     
0000018ab2fc1020 00007ffb0720ee48 767933672     

Statistics:
              MT    Count    TotalSize Class Name
00007ffb0720ee48        5   3071736072 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]][]
Total 5 objects
Fragmented blocks larger than 0.5 MB:
            Addr     Size      Followed by
00000185C6AE9878    0.6MB         00000185C6B8CAA8 System.Byte[]
00000186C6E723F0    4.1MB         00000186C72912D8 System.Byte[]
00000187C715B2B8    5.2MB         00000187C768C088 System.Byte[]
00000188C6E2E7D8    0.6MB         00000188C6ED1A08 System.Byte[]



0:000> !gcroot 0000018ae2fc1020
Thread be58:
    000000440BF3B6C0 00007FFAAAE74E00 Xw.Service.ProductCategoryService.GetParentCategoryId(Int32, System.Collections.Generic.Dictionary`2<Int32,Int32>, Int32 ByRef)
        rdi: 
            ->  00000185C6A64458 System.Collections.Generic.Dictionary`2[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]]
            ->  0000018AE2FC1020 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, System.Private.CoreLib],[System.Int32, System.Private.CoreLib]][]


看到這行代價就大概知道是什麼問題了,果斷查詢專案檔案,才發現,這個方法是一個遞迴方法。因為資料的原因導致出現了無限遞迴,所以導致System.Int32 和System.Collections.Generic.Dictionary 都很高

非常感謝部落格園裡面的大佬

https://www.cnblogs.com/huangxincheng/p/14830664.html

也是按照他的來一步步分析的。