Search code examples
visual-studiomemory-leaksperformance-testing

What is [Cycle Detected] with memory leak?


Visual Studio 2017 Community Edition

I am trying to understand/use the Performance Profiler's Memory Usage in what I feel must be a memory leak in my application (MVVM with custom controls). Three snapshots were taken:

  1. Before opening my suspect user control, NewProgressNoteView.xaml.
  2. At the time of running the user control, and
  3. After exiting the NewProgressNoteView.xaml.

I then compared snapshot #3 to snapshot #1. In the resulting table, I imposed a filter of "NewProgressNoteView". The below is the results of expanding the instance of the top Doctor_Desk.Views.NewProgressNoteView. Of note is '[Cycle Detected]' which feels suspicious, but I do not know what it means exactly or how to use this information to fix the memory leak(s)? What do I do next?

Any help would be most appreciated.

TIA.

enter image description here


Solution

  • What Is [Cycle Detected] when viewing managed memory?

    When viewing Heap snapshots inside Visual Studios Diagnostic tools you have:

    The Object Type Window which shows objects held in memory.

    When you select a particular Object Type, you can access:

    • Paths to Root - Don't be fooled by the fact this information is presented in a tree-view which usually denotes children. Paths to root actually shows Parent objects that reference the object you selected.
    • Referenced Types - Shows the child types referenced by the selected object.

    Both these tabs are reference views which help you trace an objects parent and child references presented via an expanding-tree menu.

    Cycle Detected simply means that the memory analyzer has found the parent or child reference directly or indirectly references back to your selected object. So rather than display a circular tree-view trace it adds a cycle detected tag.

    This isn't neccessarily a bad thing or your root issue.

    For further reading about analyzing memory and an expanded explanation of reference views check out this msdn article: https://learn.microsoft.com/en-us/visualstudio/profiling/analyze-memory-usage?view=vs-2019