C#内存转储,或称为dump文件,是应用程序在特定时刻状态的快照。它们对于诊断问题(如性能问题、崩溃和内存泄漏)非常有帮助。本文将向您介绍什么是内存转储,如何生成它们,以及如何分析它们。
内存转储实质上是一个应用程序在特定点状态的快照。它包含了与应用程序相关的系统内存中的所有内容,包括变量、线程和堆栈跟踪。当进行调试,特别是尝试复制难以重现的错误时,这些数据可能会非常有用。
有多种方式可以为 .NET 应用程序生成内存转储。一种常见的方法是使用任务管理器或 Process Explorer。另一种方式是使用像 DebugDiag 这样的工具或 CLRMD 这样的库来以编程方式实现。
运行你的应用程序。
打开任务管理器并转到 进程 选项卡。
右键点击你的进程并选择 "创建转储文件"。
几分钟之后,弹出窗口将显示转储文件的位置。
从 Microsoft 的网站下载并安装 Debug Diagnostic Tool 2 Update 2 (DebugDiag) 。
运行该工具并点击 "添加规则" 按钮。
选择适合你需要的规则类型(例如,崩溃,性能,或手动)。
选择你想要监控的进程。
设置规则的具体内容(如收集的转储数量),然后激活它。
分析内存转储需要专门的工具,最常用的是 Visual Studio 和 WinDbg。
打开 Visual Studio。
转到 文件 > 打开 > 项目/解决方案。
导航到你的 .dmp 文件并打开它。
Visual Studio 将自动分析崩溃转储并突出显示重要信息。
WinDbg 是一个更高级的工具,但它允许对内存转储进行深度分析:
下载并安装包含 WinDbg 的 Windows 调试工具。
打开 WinDbg,并加载你的转储文件(文件 > 打开崩溃转储)。
加载调试 .NET 应用程序所需的扩展,如 SOS (!loadby sos clr)。
使用各种命令来分析转储(例如,使用 !clrstack 来查看管理堆栈)。
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
-
- publicclassProgram
- {
- [Flags]
- publicenum Option : uint
- {
- // These options are omitted for brevity, see MSDN for more details.
- MiniDumpWithFullMemory = 0x00000002,
- MiniDumpWithHandleData = 0x00000004
- }
-
- [StructLayout(LayoutKind.Sequential, Pack = 4)]
- publicstruct MiniDumpExceptionInformation
- {
- publicuint ThreadId;
- public IntPtr ExceptionPointers;
- [MarshalAs(UnmanagedType.Bool)]
- publicbool ClientPointers;
- }
-
- [DllImport("dbghelp.dll", SetLastError = true)]
- public static extern bool MiniDumpWriteDump(IntPtr hProcess,
- uint processId,
- SafeHandle hFile,
- uint dumpType,
- ref MiniDumpExceptionInformation expParam,
- IntPtr userStreamParam,
- IntPtr callbackParam);
-
- public static void Main()
- {
- CreateDump();
- }
-
- public static void CreateDump()
- {
- // Get the current running process.
- Process currentProcess = Process.GetCurrentProcess();
-
- // Create an ExceptionInformation object
- MiniDumpExceptionInformation eInfo = new MiniDumpExceptionInformation();
- eInfo.ThreadId = (uint)currentProcess.Threads[0].Id;
- eInfo.ExceptionPointers = Marshal.GetExceptionPointers();
- eInfo.ClientPointers = false;
-
- string filePath = @"C:\temp\dumpfile.dmp";
- using (FileStream stream = new FileStream(filePath, FileMode.Create))
- {
- bool success = MiniDumpWriteDump(currentProcess.Handle, (uint)currentProcess.Id, stream.SafeFileHandle, (uint)Option.MiniDumpWithFullMemory, ref eInfo, IntPtr.Zero, IntPtr.Zero);
- if (!success)
- {
- thrownew Exception("MiniDumpWriteDump failed");
- }
- }
- }
- }
在 .NET Core 中,由于它是跨平台的,你无法直接使用 MiniDumpWriteDump 这个 Windows API 函数。但你可以使用一些第三方库来实现这个功能。
首先,需要在项目中安装
Microsoft.Diagnostics.NETCore.Client 库。你可以通过 NuGet 包管理器或者以下命令进行安装:
- dotnet add package Microsoft.Diagnostics.NETCore.Client
代码:
- using Microsoft.Diagnostics.NETCore.Client;
-
- namespaceDumpDemo
- {
- internalclassProgram
- {
- static void Main(string[] args)
- {
- int processId = int.Parse(args[0]); // The process ID to collect the dump from
- string dumpFilePath = args[1]; // The path where the dump file should be written
-
- CreateDump(processId, dumpFilePath);
- }
-
- public static void CreateDump(int processId, string dumpFilePath)
- {
- var client = new DiagnosticsClient(processId);
-
- // Dumper.CollectDump(Process process, string dumpFileName, DumpTypeOption type)
- client.WriteDump(DumpType.Normal, dumpFilePath);
- }
- }
- }
DumpType枚举:
- public enum DumpType
- {
- Normal = 1,
- WithHeap = 2,
- Triage = 3,
- Full = 4
- }
各个枚举值的含义如下:
这段代码将会生成一个包含当前进程内存转储的 .dmp 文件。请确保目标文件夹有写入权限。