龙空技术网

如何排查 .NET 内存泄漏

DotNET技术圈 108

前言:

此刻兄弟们对“netout”大概比较珍视,大家都想要分析一些“netout”的相关内容。那么小编在网摘上收集了一些对于“netout””的相关资讯,希望咱们能喜欢,咱们快快来了解一下吧!

内存泄漏通常表示:一个应用程序的某些对象在完成它的的生命周期后,由于它被其他对象意外引用,导致后续gc无法对它进行回收,长此以往就会导致程序性能的下降以及潜在的 OutOfMemoryException。

这篇我们通过一个内存泄漏工具对 .NET Core 程序进行内存泄漏分析,如果程序是跑在windows上,那直接可以使用 Visual Studio 进行诊断。

检查托管内存使用

在开始分析内存泄漏之前,你一定要有证据证明真的存在内存泄漏,这里可以用 dotnet-counters 来看下应用程序的各个指标来进行验证。

将程序跑起来

dotnet run

找出程序的 pid,

dotnet-counters ps

4807 DiagnosticScena /home/user/git/samples/core/diagnostics/DiagnosticScenarios/bin/Debug/netcoreapp3.0/DiagnosticScenarios

使用 monitor 启动监视器,这里的 --refresh-interval 表示刷新间隔

dotnet-counters monitor --refresh-interval 1 -p 4807

Press p to pause, r to resume, q to quit.

Status: Running

[System.Runtime]

# of Assemblies Loaded 118

% Time in GC (since last GC) 0

Allocation Rate (Bytes / sec) 37,896

CPU Usage (%) 0

Exceptions / sec 0

GC Heap Size (MB) 4

Gen 0 GC / sec 0

Gen 0 Size (B) 0

Gen 1 GC / sec 0

Gen 1 Size (B) 0

Gen 2 GC / sec 0

Gen 2 Size (B) 0

LOH Size (B) 0

Monitor Lock Contention Count / sec 0

Number of Active Timers 1

ThreadPool Completed Work Items / sec 10

ThreadPool Queue Length 0

ThreadPool Threads Count 1

Working Set (MB) 83

重点看一下 GC Heap Size (MB) 指标,可以看到程序启动后当前GC堆内存为 4M,打开链接: 后再看看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 -stat

Statistics:

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 00007faddaa50f90

Address MT Size

...

00007f6ad09421f8 00007faddaa50f90 94

...

00007f6ad0965b20 00007f6c1dc00f90 80

00007f6ad0965c10 00007f6c1dc00f90 80

00007f6ad0965d00 00007f6c1dc00f90 80

00007f6ad0965df0 00007f6c1dc00f90 80

00007f6ad0965ee0 00007f6c1dc00f90 80

Statistics:

MT Count TotalSize Class Name

00007f6c1dc00f90 206770 19494060 System.String

Total 206770 objects

接下来可以用 !gcroot 查看某一个string到底被谁持有着?

> gcroot -all 00007f6ad09421f8

Thread 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.String

HandleTable:

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.String

Found 2 roots.

从string的引用链看,它是被 CustomerCache 所持有,然后就可以到代码中寻找问题啦。

标签: #netout