主頁 > .NET開發 > 記一次排查線上程式記憶體的忽高忽低,又是大集合惹禍了

記一次排查線上程式記憶體的忽高忽低,又是大集合惹禍了

2020-09-14 09:13:29 .NET開發

一:背景

1. 講故事

昨天繼續還技術債,優化一輪后的程式拉到線上后記憶體繼續忽高忽低,低的時候20G,高的時候30G,過了一會又下降了幾個G,毫無疑問,程式中有什么集合或者什么操作占用了大量記憶體,所以準備在28,29G的時候抓dump分析分析,

二:解決思路

從快照中找問題就像看病一樣,根據病象推測,都有一套經驗可循,

1. 把托管堆中>10M的物件找出來

通常應對大集合從托管堆入手最簡單,看哪個型別占用空間大,基本就是它出問題了,為了避免把所有型別都打出來,這里設定一下過濾,把小于10M都踢掉, 可以用 !dumpheap -stat -min 10240,把敏感物件脫敏掉,


0:000> !dumpheap -stat -min 10240
Statistics:
              MT    Count    TotalSize Class Name
00007ffe094e6fc0        4       523776 System.Object[]
00007ffe094e6948        6      7179822 System.String
00007ffe0780da08       33     46514160 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[System.Collections.Generic.HashSet`1[[System.Int32, mscorlib]], System.Core]][]
00007ffe09f95f40      250    188739344 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[System.Int32, mscorlib]][]
00007ffe094ec988       18    540828823 System.Byte[]
00007ffe07802da8     1620    622578672 System.Linq.Set`1+Slot[[System.Int32, mscorlib]][]
000001bc0452e600     1389   1038494910      Free
00007ffe094baf50       68   1128274800 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[System.DateTime, mscorlib]][]
00007ffe094e9220     2224   1513951832 System.Int32[]
00007ffe07819df8     2232   1668042480 System.Collections.Generic.HashSet`1+Slot[[System.Int32, mscorlib]][]
00007ffe094c8510      226   1672164568 System.Int64[]
00007ffdab8676e8     1137   1901228880 System.Collections.Generic.HashSet`1+Slot[[System.Int64, mscorlib]][]
00007ffdab89b3b0      136   1986723840 System.Linq.Set`1+Slot[[System.Int64, mscorlib]][]
Total 13321 objects

2. 找出堆中可疑的物件

因為程式啟動后作為記憶體資料庫,所以有包含指定類的大集合物件很正常,倒數第7行有一個Dictionary<int,Datetime> 占用空間挺大的,1128274800/1024/1024=1G,這個貌似不是基礎資料,應該是中間變數,方法表地址為00007ffe094baf50, 通過它可以找到這68個集合的記憶體地址,


0:028> !dumpheap -mt 00007ffe094baf50
         Address               MT     Size   
000001c2f262a708 00007ffe094baf50 69438000     
000001c1bb8e1020 00007ffe094baf50 16147872     
000001c1bce04760 00007ffe094baf50 33486336     
000001c37e8f1020 00007ffe094baf50 143987328     
000001c44e8f1020 00007ffe094baf50 287974800    
000001c3c419b268 00007ffe094baf50 16147872   
000001c3f6b9ac28 00007ffe094baf50 16147872     
000001c467336fa0 00007ffe094baf50 33486336     
000001c46f3fa760 00007ffe094baf50 69438000   
000001c489df3668 00007ffe094baf50 16147872     
000001c494166828 00007ffe094baf50 33486336     
000001c4a68f1020 00007ffe094baf50 69438000  
000001c4d4c5c290 00007ffe094baf50 16147872     
000001c4da8f1058 00007ffe094baf50 33486336     
000001c4de8f1020 00007ffe094baf50 69438000
000001c5028f1058 00007ffe094baf50 33486336     
000001c5068f1020 00007ffe094baf50 33486336
...

下一步挑幾個大的 Dictionary 看看,比如這一行: 000001c44e8f1020 00007ffe094baf50 287974800,計算一下size:279M,

3. 尋找集合所在的代碼塊

字典占用279M我是知道了,但怎么知道這個字典是在哪一個代碼塊呢? 要尋找答案也容易,通過!gcroot 找到它的參考根,通過參考鏈就可以找到它的代碼區塊,簡直不要太實用,??????,


0:000> !gcroot 000001c4de8f1020 
Thread 2da8:
    00000017f4c7e5d0 00007ffdab758ca1 xxxx.xxxx.xxxx.GetFlowAwayCustomer(Int32, System.String, System.Collections.Generic.Dictionary`2<System.String,System.Collections.Generic.List`1<xxxx>>)
        rbp-238: 00000017f4c7e628
            ->  000001c3d5c1bdf0 System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]], mscorlib]]
            ->  000001c3d8de7d10 System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]], mscorlib]][]
            ->  000001c3d8d58630 System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]]
            ->  000001c4de8f1020 System.Collections.Generic.Dictionary`2+Entry[[System.Int32, mscorlib],[System.DateTime, mscorlib]][]

從上面參考鏈可以看到三點資訊:

<1> 當前字典在 2da8 執行緒上

<2> 字典在 GetFlowAwayCustomer 方法中,大概可以看出是計算流失客戶的,

<3> 呼叫鏈頂部是最大的集合 Dictionary<string,Ditionary<int,DateTime>> ,address:000001c3d5c1bdf0

4. 尋找更多資訊

<1> 挖字典內容

有了最大的字典,我們來看看最大字典Dictionary<string,Ditionary<int,DateTime>> 占用的記憶體大小,


0:000> !objsize 000001c3d5c1bdf0 
sizeof(000001c3d5c1bdf0) = 340008256 (0x14441d40) bytes (System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]], mscorlib]])

根據sizeof(000001c3d5c1bdf0) = 340008256 (0x14441d40) bytes 計算一下:324M,尼瑪,這都是其中一個字典,難怪記憶體忽高忽低,現在大家肯定特別想知道里面有啥東西,可以用 da -> !do 去內部集合看一下,


0:000> !da -length 1 -start 1 -details 000001c3d8de7d10
Name:        System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]], mscorlib]][]
MethodTable: 00007ffdab940650
EEClass:     00007ffdab9405b8
Size:        192(0xc0) bytes
Array:       Rank 1, Number of elements 7, Type VALUETYPE
Element Methodtable: 00007ffdab940520
[1] 000001c3d8de7d38
    Name:        System.Collections.Generic.Dictionary`2+Entry[[System.String, mscorlib],[System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]], mscorlib]]
    MethodTable: 00007ffdab940520
    EEClass:     00007ffe08e92920
    Size:        40(0x28) bytes
    File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
    Fields:
                      MT    Field   Offset                 Type VT     Attr            Value Name
        00007ffe094e9288  4003474       10             System.Int32      1     instance             58671583     hashCode
        00007ffe094e9288  4003475       14             System.Int32      1     instance                   -1     next
        00007ffe094ebf10  4003476        0           System.__Canon      0     instance     000001c2cec43610     key
        00007ffe094ebf10  4003477        8           System.__Canon      0     instance     000001c3d7b45370     value
0:000> !do 000001c3d7b45370     
Name:        System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]]
MethodTable: 00007ffe094b9ec8
EEClass:     00007ffe08e9d528
Size:        80(0x50) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_64\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
Fields:
              MT    Field   Offset                 Type VT     Attr            Value Name
00007ffe094e9220  4001858        8       System.Int32[]  0 instance 000001c46e8f1020 buckets
00007ffe094baf50  4001859       10 ...ime, mscorlib]][]  0 instance 000001c46f3fa760 entries
00007ffe094e9288  400185a       38         System.Int32  1 instance          2512598 count
00007ffe094e9288  400185b       3c         System.Int32  1 instance          3194430 version
00007ffe094e9288  400185c       40         System.Int32  1 instance               -1 freeList
00007ffe094e9288  400185d       44         System.Int32  1 instance                0 freeCount
00007ffe094dabb8  400185e       18 ...Int32, mscorlib]]  0 instance 000001bc06272ab8 comparer
00007ffe0a0463e0  400185f       20 ...eTime, mscorlib]]  0 instance 0000000000000000 keys
00007ffe0a046258  4001860       28 ...eTime, mscorlib]]  0 instance 0000000000000000 values
00007ffe094e6f28  4001861       30        System.Object  0 instance 0000000000000000 _syncRoot

可以看到大字典中7個元素,然后我挑了一個內嵌Dictionary,可以看到這個內嵌字典的count=251w,里面的details我就不輸出了,

<2> 挖執行緒堆疊

有了字典內容,大家繼續看一下此時這個執行緒 [2da8] 在做什么?


0:028> ~~[2da8]s
ntdll!NtWaitForSingleObject+0x14:
00007ffe`28646124 c3              ret
0:028> !clrstack 
OS Thread Id: 0x2da8 (28)
        Child SP               IP Call Site
00000017f4c7e388 00007ffe28646124 [HelperMethodFrame: 00000017f4c7e388] 
00000017f4c7e4f0 00007ffe09e48e52 System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]].Resize(Int32, Boolean)
00000017f4c7e560 00007ffe09316c65 System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.DateTime, mscorlib]].Insert(Int32, System.DateTime, Boolean)

我靠,這個集合正在做擴容,,,大家應該知道,有擴容就有虛占記憶體,

三: 總結

到這里肯定有人問,找出大集合了,解決方案是什么? 因為是昨天才發現的,何況代碼不是我寫的,你問我哈??? 準備從兩方面入口, 業務邏輯上優化 ? 定制化集合(HashSet,Dictionary),畢竟這兩個集合虛占記憶體太可怕了,下一篇我們來分析一下他們的擴容機制,


如您有更多問題與我互動,掃描下方進來吧~


圖片名稱

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/33168.html

標籤:C#

上一篇:幫忙看下winform視窗雙快取 之后,內部控制元件picturebox 使用gdi+ 繪制操作無效

下一篇:[C#] 折騰海康威視的人體測溫 模組

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more