dotMemory 2025.2 Help

使用 API 控制分析会话

profiling API 提供了许多类,允许您控制分析过程。 例如,直接从您的应用程序代码中,您可以:

  • 获取内存快照: MemoryProfiler.GetSnapshot(string name)

  • 启用或禁用内存分配数据的收集: MemoryProfiler.CollectAllocations(bool enable)

  • 甚至强制进行垃圾回收: MemoryProfiler.ForceGc()

有关 API 类的详细信息,请参阅 API 参考

有两个主要场景需要使用 profiling API:

分析代码的特定部分

API 允许您缩小分析范围,仅分析您感兴趣的代码。 例如,有时很难在正确的时刻点击 获取快照 按钮。 使用 profiling API,您可以在代码的确切位置执行“获取快照”调用。

请注意,dotMemory 2018.3 及更早版本使用了另一个版本的 profiling API(有关此 API 的更多信息,请参阅 dotMemory 2018.3 文档)。 此 API 仍受支持,但我们强烈建议您使用本节中描述的最新 API 版本。

主要概念:

  • API 的源代码可在 GitHub 上获取。 代码以 Apache 许可证分发。

  • profiling API 作为 JetBrains.Profiler.API NuGet 包分发,您需要在项目中引用它。

  • 要启用 API,您必须在 profiler options 中启用 如何控制性能分析 | 使用 API 参数并启动分析会话。

  • 使用 API 时,与普通分析会话相比,唯一的区别在于您如何控制会话:您需要调用相应的 API 方法,而不是点击 profiling controller 中的按钮。

  • 如果您在没有分析的情况下运行应用程序,对 API 方法的调用将被忽略。 API 不会抛出任何错误。

  • 控制分析会话的主要类是 MemoryProfiler 静态类。

  • 要获取内存快照并将其保存到磁盘,请调用 MemoryProfiler.GetSnapshot(string name)——相当于按下 获取快照 按钮。

  • 要启用/禁用内存分配数据的收集,请调用 CollectAllocations(bool enable)——相当于按下 收集分配 按钮。

  • 要检查 CollectAllocations(bool enable) 调用是否会生效,请使用 MemoryProfiler.GetFeatures()。 请注意,此检查不是强制性的。

要使用 API 分析代码的特定部分

  1. 在项目中引用 JetBrains.Profiler.API NuGet 包。

  2. 根据您的用例需要,将 MemoryProfiler 类的方法调用插入到代码中。 例如:

    private void SomeMethod() { // Enable collecting memory allocation data MemoryProfiler.CollectAllocations(true); ...// Here goes some code that I want to profile // Get a snapshot MemoryProfiler.GetSnapshot("my snapshot"); }
  3. 从 dotMemory 开始分析应用程序,并在 profiler options 中选择 如何控制性能分析 | 使用 API 参数。

创建自分析应用程序

顾名思义,在此场景中,应用程序会自行分析。 与之前的场景相比,主要区别在于如何启动分析。 如果您使用 profiling API 分析代码的特定部分,您需要手动启动会话(例如,使用 dotMemory 界面)。 对于自分析应用程序,会话是直接从应用程序代码中启动的。

主要概念:

向应用程序添加自分析功能

  1. 在项目中引用 JetBrains.Profiler.SelfApi NuGet 包。

  2. 通过调用 DotMemory.Init() 方法初始化自分析 API。 请注意,首次初始化可能需要一些时间,因为 API 需要下载 dotMemory.exe 命令行工具。 如果您希望跟踪下载过程并能够取消下载,请直接使用 DotMemory.InitAsync() 方法。 它允许您使用 CancellationToken 并通过回调变量跟踪进度。

  3. 根据您的用例需要添加 API 调用:

    • 如果您只需要一个快照:

      static void Main(string[] args) { ... // Here goes some init code // Initialize the API and download dotMemory.exe (if needed) DotMemory.Init(); SomeMethod(); } private void SomeMethod() { ... // Here goes some code that I want to profile // Get a snapshot and save it to Temp var config = new DotMemory.Config(); config.SaveToDir("C:\\Temp\\Workspace"); DotMemory.GetSnapshotOnce(config); }
    • 如果您需要多个快照:

      static void Main(string[] args) { ... // Here goes some init code // Initialize the API and download dotMemory.exe (if needed) DotMemory.Init(); // create profiling config and attach to the current process var config = new DotMemory.Config(); config.SaveToDir("C:\\Temp\\Workspace"); DotMemory.Attach(config); SomeMethod(collection); // Detach from the current process DotMemory.Detach(); } private void SomeMethod(IEnumerable<String> collection) { foreach (var item in collection) { ... // Here goes some code that I want to profile // Get a snapshot DotMemory.GetSnapshot("my snapshot"); } }
最后修改日期: 2025年 9月 27日