使用 API 控制分析会话
profiling API 提供了许多类,允许您控制分析过程。 例如,直接从您的应用程序代码中,您可以:
获取内存快照:
MemoryProfiler.GetSnapshot(string name),启用或禁用内存分配数据的收集:
MemoryProfiler.CollectAllocations(bool enable),甚至强制进行垃圾回收:
MemoryProfiler.ForceGc(),
有关 API 类的详细信息,请参阅 API 参考。
有两个主要场景需要使用 profiling API:
请注意,在这种情况下,您必须使用单独的 self-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 分析代码的特定部分
在项目中引用
JetBrains.Profiler.APINuGet 包。根据您的用例需要,将
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"); }从 dotMemory 开始分析应用程序,并在 profiler options 中选择 如何控制性能分析 | 使用 API 参数。
创建自分析应用程序
顾名思义,在此场景中,应用程序会自行分析。 与之前的场景相比,主要区别在于如何启动分析。 如果您使用 profiling API 分析代码的特定部分,您需要手动启动会话(例如,使用 dotMemory 界面)。 对于自分析应用程序,会话是直接从应用程序代码中启动的。
主要概念:
self-profiling API 是一个单独的 JetBrains.Profiler.SelfApi NuGet 包,您需要在项目中引用它。
为了控制分析会话,API 使用 dotMemory.exe 命令行工具。
dotMemory.exe 工具不是包的一部分。 当您使用
DotMemory.Init()方法初始化 API 时,API 会下载最新版本的 JetBrains.dotMemory.Console NuGet 包。默认情况下,包中的工具会保存到 %LOCALAPPDATA%\JetBrains\Profiler 文件夹。 要指定其他位置,请使用
downloadTo参数和DotMemory.Init()方法。如果 dotMemory.exe 工具的包已存在,则不会下载新的包。
要获取单个快照,请使用
DotMemory.GetSnapshotOnce()方法。要获取多个快照,您需要:
使用
DotMemory.Attach()附加到当前进程使用
DotMemory.GetSnapshot()获取任意数量的快照使用
DotMemory.Detach()从进程中分离
要配置分析会话,例如指定保存快照的路径,您必须使用
DotMemory.Config类的实例。您可以同时使用自分析 API 和 profiling API。
生成的自分析快照具有
.dmw文件扩展名(实际上是一个 dotMemory 工作区)。
向应用程序添加自分析功能
在项目中引用
JetBrains.Profiler.SelfApiNuGet 包。通过调用
DotMemory.Init()方法初始化自分析 API。 请注意,首次初始化可能需要一些时间,因为 API 需要下载 dotMemory.exe 命令行工具。 如果您希望跟踪下载过程并能够取消下载,请直接使用DotMemory.InitAsync()方法。 它允许您使用CancellationToken并通过回调变量跟踪进度。根据您的用例需要添加 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"); } }