如何排查 .NET 内存泄漏
內存泄漏通常表示:一個應用程序的某些對象在完成它的的生命周期后,由于它被其他對象意外引用,導致后續gc無法對它進行回收,長此以往就會導致程序性能的下降以及潛在的 OutOfMemoryException。
這篇我們通過一個內存泄漏工具對 .NET Core 程序進行內存泄漏分析,如果程序是跑在windows上,那直接可以使用 Visual Studio 進行診斷。
檢查托管內存使用
在開始分析內存泄漏之前,你一定要有證據證明真的存在內存泄漏,這里可以用 dotnet-counters 來看下應用程序的各個指標來進行驗證。
將程序跑起來
找出程序的 pid,
使用 monitor 啟動監視器,這里的 --refresh-interval 表示刷新間隔
重點看一下 GC Heap Size (MB) 指標,可以看到程序啟動后當前GC堆內存為 4M,打開鏈接:https://localhost:5001/api/diagscenario/memleak/20000 ?后再看看GC堆內存,可以看到一下子就到 30M 了,如下所示:
GC?Heap?Size?(MB)?????????????????????????????????30通過對比內存的使用,這下子可以拍胸脯的說,確認內存內存泄漏。
生成dump文件
要想分析程序的內存泄漏,首先要有訪問GC堆的權限,這樣就可以分析heap內存以及對象之間的關系,然后就可以大膽猜想為啥內存沒有得到釋放?要想生成 .NET Core 程序的dump文件,可以借助 dotnet-dump 工具。
dotnet-dump?collect?-p?4807 Writing?minidump?with?heap?to?./core_20190430_185145 Complete分析 core dump
接下來可以使用 dotnet-dump analyze 對已生成的dump文件進行分析。
dotnet-dump?analyze?core_20190430_185145這里的 core_20190430_185145 就是你想要分析的dump名,值得一提的是:如果你遇到了 libdl.so cannot be found 錯誤,建議以安裝一下 ?libc6-dev package 包。
首先我們通過 sos 命令查看 托管堆 上的所有對象統計清單。
>?dumpheap?-statStatistics:MT????Count????TotalSize?Class?Name ... 00007f6c1eeefba8??????576????????59904?System.Reflection.RuntimeMethodInfo 00007f6c1dc021c8?????1749????????95696?System.SByte[] 00000000008c9db0?????3847???????116080??????Free 00007f6c1e784a18??????175???????128640?System.Char[] 00007f6c1dbf5510??????217???????133504?System.Object[] 00007f6c1dc014c0??????467???????416464?System.Byte[] 00007f6c21625038????????6??????4063376?testwebapi.Controllers.Customer[] 00007f6c20a67498???200000??????4800000?testwebapi.Controllers.Customer 00007f6c1dc00f90???206770?????19494060?System.String Total?428516?objects從輸出看,大頭都是些 String 和 Customer 對象,然后可以通過 mt 參數來獲取該方法表下所有的實例對象。
>?dumpheap?-mt?00007faddaa50f90Address???????????????MT?????Size ... 00007f6ad09421f8?00007faddaa50f90???????94 ... 00007f6ad0965b20?00007f6c1dc00f90???????80 00007f6ad0965c10?00007f6c1dc00f90???????80 00007f6ad0965d00?00007f6c1dc00f90???????80 00007f6ad0965df0?00007f6c1dc00f90???????80 00007f6ad0965ee0?00007f6c1dc00f90???????80Statistics:MT????Count????TotalSize?Class?Name 00007f6c1dc00f90???206770?????19494060?System.String Total?206770?objects接下來可以用 !gcroot 查看某一個string到底被誰持有著?
>?gcroot?-all?00007f6ad09421f8Thread?3f68:00007F6795BB58A0?00007F6C1D7D0745?System.Diagnostics.Tracing.CounterGroup.PollForValues()?[/_/src/System.Private.CoreLib/shared/System/Diagnostics/Tracing/CounterGroup.cs?@?260]rbx:??(interior)->??00007F6BDFFFF038?System.Object[]->??00007F69D0033570?testwebapi.Controllers.Processor->??00007F69D0033588?testwebapi.Controllers.CustomerCache->??00007F69D00335A0?System.Collections.Generic.List`1[[testwebapi.Controllers.Customer,?DiagnosticScenarios]]->??00007F6C000148A0?testwebapi.Controllers.Customer[]->??00007F6AD0942258?testwebapi.Controllers.Customer->??00007F6AD09421F8?System.StringHandleTable:00007F6C98BB15F8?(pinned?handle)->?00007F6BDFFFF038?System.Object[]->?00007F69D0033570?testwebapi.Controllers.Processor->?00007F69D0033588?testwebapi.Controllers.CustomerCache->?00007F69D00335A0?System.Collections.Generic.List`1[[testwebapi.Controllers.Customer,?DiagnosticScenarios]]->?00007F6C000148A0?testwebapi.Controllers.Customer[]->?00007F6AD0942258?testwebapi.Controllers.Customer->?00007F6AD09421F8?System.StringFound?2?roots.從string的引用鏈看,它是被 CustomerCache 所持有,然后就可以到代碼中尋找問題啦。
總結
以上是生活随笔為你收集整理的如何排查 .NET 内存泄漏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .NET Core etcd 配置源
- 下一篇: 记一次 .NET 某云采购平台API 挂