title | description | ms.date | ms.topic | dev_langs | author | ms.author | manager | ms.subservice | ||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Analyze memory usage in the Performance Profiler |
Learn how to use the Memory Usage tool without the debugger in the Visual Studio Performance Profiler to monitor your app's memory use. |
02/06/2024 |
how-to |
|
mikejo5000 |
mikejo |
mijacobs |
debug-diagnostics |
The Memory Usage tool monitors your app's memory use. You can use the tool to study the real-time memory effects of scenarios you're actively developing in Visual Studio. You can take detailed snapshots of the app's memory states, and compare snapshots to find the root causes of memory issues. The Memory Usage tool is supported on .NET, ASP.NET, C++, or mixed mode (.NET and native) apps.
The Memory Usage tool can run with or without the debugger. In this article, we show how to use the Memory Usage tool without the debugger in the Visual Studio Performance Profiler, which is recommended for release builds. For information on choosing the best memory analysis tool for your needs, see Choose a memory analysis tool.
To start a Memory Usage diagnostic session:
-
Open a project in Visual Studio.
The Memory Usage tool supports .NET, ASP.NET, C++, or mixed mode (.NET and native) apps.
-
In the Debug menu, set the solution configuration to Release and select Local Windows Debugger (or Local Machine) as the deployment target.
-
On the menu bar, select Debug > Performance Profiler.
-
Under Available Tools, select Memory Usage, and then select Start.
When you start a diagnostic session, your app starts, and the Diagnostic Tools window displays a timeline graph of your app's memory use.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="vs-2019"
::: moniker-end
The timeline graph shows memory fluctuations as the app runs. Spikes in the graph usually indicate that some code is collecting or creating data, and then discarding it when the processing is done. Large spikes indicate areas that you can optimize. Main concern is a rise in memory consumption that's not returned. This may indicate inefficient memory use or even a memory leak.
An app uses a large number of objects, and you might want to concentrate your analysis on one scenario. Or, you may find memory issues to investigate. You can take snapshots during a diagnostic session to capture memory usage at particular moments. It's good to get a baseline snapshot of an app before a memory issue appears. You can take another snapshot after the first occurrence of the problem, and additional snapshots if you can repeat the scenario.
To collect snapshots, select Take snapshot when you want to capture the memory data.
::: moniker range=">=vs-2022"
::: moniker-end
To stop a monitoring session without creating a report, just close the diagnostic window. To generate a report when you're done collecting or have taken snapshots, select Stop Collection.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="vs-2019"
::: moniker-end
If you have trouble collecting or displaying data, see Troubleshoot profiling errors and fix issues.
After you stop data collection, the Memory Usage tool stops the app and displays the Memory Usage overview page.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="vs-2019"
::: moniker-end
The numbers in the Snapshot panes show the objects and bytes in memory when each snapshot was taken, and the difference between the snapshot and the previous one.
The numbers are links that open detailed Memory Usage report views in new Visual Studio windows. A snapshot details report shows the types and instances in one snapshot. A snapshot difference (diff) report compares the types and instances in two snapshots.
For C++, the Objects (Diff) column is named Allocations (Diff).
[!INCLUDE managed-types-report]
Many types in apps aren't required to app developers. The snapshot report filters can hide most of these types in the Managed Memory and Paths to Root trees.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="vs-2019"
::: moniker-end
-
To filter a tree by type name, enter the name in the Filter box. The filter isn't case-sensitive, and it recognizes the specified string in any part of the type name.
-
Select Show Just My Code in the Filter dropdown to hide most instances that are generated by external code. External types belong to the operating system or framework components, or are generated by the compiler.
-
Select Collapse Small Objects in the Filter dropdown to hide types whose Size (Bytes) is less than 0.5 percent of the total memory.
[!INCLUDE native-types-report]
::: moniker range=">=vs-2022"
[!INCLUDE memory-usage-insights] ::: moniker-end
-
Choose the change link in a cell of the Snapshot pane in the Memory Usage overview page.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="vs-2019"
::: moniker-end
-
Choose a snapshot in the Compare To list of a managed or native report.
::: moniker range=">=vs-2022"
::: moniker-end
::: moniker range="<=vs-2019"
::: moniker-end
[!INCLUDE change-diff-report]