How to read the memory snapshot in Visual Studio

2019-02-11 23:59发布

问题:

I use Visual Studio to take memory snapshot of my application.

I have some questions about understanding the data I got. I after I capture the memory snapshot, I filter out one of my class, say MyClassPanel. I only use MyClassPanel in my xmal files.

  1. why ‘Paths to Root’ for MyClassPanel are all ‘MyClassPanel [RefCount Handle, Count:1]’? (i.e. same name but with a string [RefCount Handle at the end] and a different Count value) what does this mean? In other class, I filter, I see the 'Paths to Root' are different class names.

  2. I have 24 MyClassPanel (from the count in top table). And in the ‘Reference Count’ column in the bottom table, when I add them up, they are 24.

Is that always the case? I think ‘Count’ and ‘Reference Count’ means different things. In this case, they add up. Does this make sense?

回答1:

  1. The Paths to Root view shows the references to this type keeping it from being garbage collected. Since your class is a Xaml page, the reference which keeps the class alive is a CLR handler for the Xaml page. These show up as RefCount Handle.

  2. Count and reference count are indeed not the same. Count is the number of instances, reference count the number of references. Because each instance in your case only has one reference, it makes sense they add up.

For more info: http://blogs.msdn.com/b/visualstudioalm/archive/2013/06/20/using-visual-studio-2013-to-diagnose-net-memory-issues-in-production.aspx http://blogs.msdn.com/b/visualstudioalm/archive/2013/10/16/net-memory-analysis-enhancements-in-visual-studio-2013.aspx



回答2:

I am not a big fan of Visual Studio Snapshot analyzer. There are quite a few complications available if finalization is not properly executed before taking the snapshot. I am not sure how Visual Studio handles that. However, the snapshot you have provided doesn't make much sense for me either.

I would suggest you to download the ANTS memory profiler and investigate this. It comes with a reasonable trial period. By using that, you will be able to see all references to each instance by using it's 'Retention Graph'. It will specifically show you which instances keeps the reference to your objects and also it will show you which objects have implemented Dispose but hasn't call. I guess, it will provide more support to find the root cause for this issue.

Have a look at below pages walkthrough

http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/walkthrough http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/