Skip to content

Latest commit

 

History

History
193 lines (125 loc) · 12 KB

memory-usage-without-debugging2.md

File metadata and controls

193 lines (125 loc) · 12 KB
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
CSharp
VB
FSharp
C++
mikejo5000
mikejo
mijacobs
debug-diagnostics

Analyze memory usage without debugging in the Performance Profiler (C#, Visual Basic, C++, F#)

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.

Memory Usage diagnostic sessions

To start a Memory Usage diagnostic session:

  1. Open a project in Visual Studio.

    The Memory Usage tool supports .NET, ASP.NET, C++, or mixed mode (.NET and native) apps.

  2. In the Debug menu, set the solution configuration to Release and select Local Windows Debugger (or Local Machine) as the deployment target.

  3. On the menu bar, select Debug > Performance Profiler.

  4. Under Available Tools, select Memory Usage, and then select Start.

    ::: moniker range=">=vs-2022" Start a Memory Usage diagnostic session. ::: moniker-end

    ::: moniker range="vs-2019" Start a Memory Usage diagnostic session. ::: moniker-end

Monitor memory use

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" Screenshot of the Diagnostic Tools window in the Visual Studio Performance Profiler showing a timeline graph of the app's memory use. ::: moniker-end

::: moniker range="vs-2019" Screenshot of the Diagnostic Tools window in the Visual Studio Performance Profiler showing a timeline graph of the app's memory use. ::: 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.

Take snapshots of app memory states

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" Screenshot of taking a snapshot. ::: moniker-end

Close the diagnostic session

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" Screenshot of stopping the collection. ::: moniker-end

::: moniker range="vs-2019" Screenshot of stopping the collection. ::: moniker-end

If you have trouble collecting or displaying data, see Troubleshoot profiling errors and fix issues.

Memory Usage reports

After you stop data collection, the Memory Usage tool stops the app and displays the Memory Usage overview page.

::: moniker range=">=vs-2022" Screenshot of the overview page in the Memory Usage tool in the Visual Studio Performance Profiler, showing a memory usage graph and two snapshot panes. ::: moniker-end

::: moniker range="vs-2019" Screenshot of the overview page in the Memory Usage tool in the Visual Studio Performance Profiler, showing a memory usage graph and two snapshot panes. ::: moniker-end

Memory Usage snapshots

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.

::: moniker range=">=vs-2022" Screenshot of Snapshot view links

For C++, the Objects (Diff) column is named Allocations (Diff).

Image Description
Step 1 The total number of objects in memory when the snapshot was taken. Select this link to display a snapshot details report sorted by the count of instances of the types.
Step 2 The difference between the total number of memory objects in this snapshot and the previous snapshot. Select this link to display a snapshot diff report sorted by the difference in the total count of instances of the types.
Step 3 The total number of bytes in memory when the snapshot was taken. Select this link to display a snapshot details report sorted by the total size of the type instances.
Step 4 The difference between the total size of memory objects in this snapshot and the previous snapshot. A positive number means the memory size of this snapshot is larger than the previous one, and a negative number means the size is smaller. Baseline means a snapshot is the first in a diagnostic session. No Difference means the difference is zero. Select this link to display a snapshot diff report sorted by the difference in the total size of instances of the types.
::: moniker-end

::: moniker range="vs-2019" Snapshot view links

Image Description
Step 1 The total number of bytes in memory when the snapshot was taken. Select this link to display a snapshot details report sorted by the total size of the type instances.
Step 2 The total number of objects in memory when the snapshot was taken. Select this link to display a snapshot details report sorted by the count of instances of the types.
Step 3 The difference between the total size of memory objects in this snapshot and the previous snapshot. A positive number means the memory size of this snapshot is larger than the previous one, and a negative number means the size is smaller. Baseline means a snapshot is the first in a diagnostic session. No Difference means the difference is zero. Select this link to display a snapshot diff report sorted by the difference in the total size of instances of the types.
Step 4 The difference between the total number of memory objects in this snapshot and the previous snapshot. Select this link to display a snapshot diff report. It’s sorted by the difference in the total count of instances of the types.
::: moniker-end

Managed types reports

[!INCLUDE managed-types-report]

Report tree filters

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" Sort and filter options ::: moniker-end

::: moniker range="vs-2019" Sort and filter options ::: 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.

Native types reports

[!INCLUDE native-types-report]

::: moniker range=">=vs-2022"

Memory Usage Insights

[!INCLUDE memory-usage-insights] ::: moniker-end

Change (Diff) reports

  • Choose the change link in a cell of the Snapshot pane in the Memory Usage overview page.

    ::: moniker range=">=vs-2022"

    Screenshot of Choose a change link in a cell.

    ::: moniker-end

    ::: moniker range="vs-2019"

    Screenshot of Choose a change link in a cell.

    ::: moniker-end

  • Choose a snapshot in the Compare To list of a managed or native report.

    ::: moniker range=">=vs-2022"

    Screenshot of Choose a snapshot from the Compare with list.

    ::: moniker-end

    ::: moniker range="<=vs-2019"

    Screenshot of Choose a snapshot from the Compare To list.

    ::: moniker-end

[!INCLUDE change-diff-report]

Related content