.net内存泄漏

使用以下两个工具进行分析
dotnet-dounters
dotnet-dump

安装

dotnet tool install --global dotnet-counters
dotnet tool install --global dotnet-dump

查看进程信息并找到进程编号

dotnet-counters ps

监视进程

dotnet-counters monitor -p 14660 --refresh-interval 1 监视id为14660的进程,每隔1秒钟刷新一次。

输出含义如下
[System.Runtime]
% Time in GC since last GC (%) 0 #GC垃圾回收百分比
Allocation Rate (B / 1 sec) 73,440 #内存分配量
CPU Usage (%) 0 #CPU使用率
Exception Count (Count / 1 sec) 0 #异常数量
GC Fragmentation (%) 5.437#GC 堆碎片率
GC Heap Size (MB) 22 #GC堆内存分配量(消耗内存)
Gen 0 GC Count (Count / 1 sec) 0 #0代垃圾回收次数
Gen 0 Size (B) 1,042,696 #0代垃圾回收大小
Gen 1 GC Count (Count / 1 sec) 0 #1代垃圾回收次数
Gen 1 Size (B) 180,520 #1代垃圾回收大小
Gen 2 GC Count (Count / 1 sec) 0 #2代垃圾回收次数
Gen 2 Size (B) 2,014,768 #2代垃圾回收大小
IL Bytes Jitted (B) 448,237 #JIT编译的IL总节数
LOH Size (B) 16,875,680 #大对象占用内存(大于85000字节的对象)
Monitor Lock Contention Count (Count / 1 sec) 0 #锁竞争次数
Number of Active Timers 3 #活动计数器
Number of Assemblies Loaded 108 #程序集加载数量
Number of Methods Jitted 6,086 #JIT编译的方法总数
POH (Pinned Object Heap) Size (B) 113,608 #固定对象堆的字节数
ThreadPool Completed Work Item Count (Count / 1 sec) 2 #线程池完成任务数量
ThreadPool Queue Length 0 #线程池工作项队列长度
ThreadPool Thread Count 5 #线程池线程数量

转储内存文件

dotnet-dump collect -p 10232 (进程编号)

分析

dotnet-dump analyze dump_20210825_225811.dmp(转储文件名)

进入分析模式后查看内存占用
dumpheap -stat

分析类型具体对象
dumpheap -mt 00007ffe88612360

gcroot -all 000002c054600480 对象编号

开启GC高并发回收

项目文件中添加

<ServerGarbageCollection>false</ServerGarbageCollection>
<!-- ServerGarbageCollection : 服务器垃圾收集 :不会让内存无限增长 -->
<!-- 其中,设置true就是服务器模式,设置false就是工作站模式,当然,去掉这一行,默认也是工作站模式。-->


<ConcurrentGarbageCollection>true</ConcurrentGarbageCollection>
<!--- ServerGarbageCollection : 并发垃圾收集 :不会让内存无限增长 -->

参考
C# 性能诊断工具 dotnet-counters 的使用
调查性能计数器 (dotnet-counters)
.net最佳实践二:使用finalize/dispose模式提升垃圾回收器性能
.NET Best Practice No: 2: Improve garbage collector performance using finalize/dispose pattern
『内存泄露』在生产环境中,.Net如何定位系统内存泄露具体位置
利用dotnet-dump分析docker容器内存泄露
dotnet-dump analyze 内存泄漏分析
.net Core 内存过高问题解决方案

posted @ 2022-05-26 11:58  Hey,Coder!  阅读(361)  评论(0编辑  收藏  举报