Getting CalculateHeldBytes in code

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.

Moderator: SciTech Software

Getting CalculateHeldBytes in code

Postby inquistor » Wed Jul 13, 2016 2:58 pm

Hi,

I'm new to MemProfiler and I'm trying obtain the values from a saved Snapshot using the API.
So far, I'm pretty much doing what's described in the second code snippet in this post http://forum.memprofiler.com/viewtopic.php?t=1317.
Doing so, I get the values for LiveInstanceCount and LiveByteCount.
In addition, I'd like to have the HeldBytesCount which for me is always null right now (even though there are values shown in the GUI). If I understand it correctly those values have to be calculated but (if the CalculateHeldBytes function is the right thing to use here at all) appearently I'm using it wrong.

Thank you in advance and a code example for reference would be really appreciated as I couldn't find any related question.

Thanks and best regards
inquistor
 
Posts: 3
Joined: Wed Jul 13, 2016 2:38 pm

Re: Getting CalculateHeldBytes in code

Postby Andreas Suurkuusk » Thu Jul 14, 2016 9:53 am

The CalculateHeldBytes method can be used to calculate the held bytes for separate instances. You can call it for a single instance using CalculateHeldBytes( typeInstanceId, null ) and it will return the number of held instances and bytes for the instance. You can also provide a collection of instances to CalculateHeldBytes, but then no data is returned. Instead you can access the ProfilerComparison.HeldInstances dictionary to retrieve the held bytes data for the instances.

However, as I understand it, you actually want the "combined" held bytes information for the types returned by GetComparedTypes. Is that correct?

This information is not calculated when comparing snapshots, instead it is calculated by the comparison analyser. There's currently no public interface to explicitly calculate the held bytes information for the types, but you can force the calculation of the held bytes information by running the analyser.

The code snippet below shows how you can run the analyser (unfortunately it is only possible to exclude analysers, so you cannot specify that you want to only run the held bytes analyser).

Code: Select all
// Ignore the duplicate instances analyser, since it can take a very long
// time to perform. If duplicates instances information is wanted, do not Add
// the DuplicateInstance issue id to the ignored issues.
IgnoredIssuesCollection ignoredIssues = new IgnoredIssuesCollection();
ignoredIssues.Add( new IgnoredIssue( InstanceIssueIds.DuplicateInstance ) );

// Analyse the comparison to get information about any issues.
AnalysisResult analysisResult = comparison.AnalyseComparison(
    new AnalysisSettings { IgnoreRuntimeIssues = true }, // Ignore runtime issues
    new IgnoredIssuesCollection[] { ignoredIssues },
    null // No pre-allocated AnalysisResult
    );


After the analyser is finished, you will be able to retrieve the held bytes information from the compared types.

I hope this helps. If you need more information or a full sample, please reply to this post.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 964
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Re: Getting CalculateHeldBytes in code

Postby inquistor » Wed Jul 20, 2016 7:28 am

Hi,

thanks for your reply and sorry for answering it so late.
After running the Analyser the HeldBytes count for me is still null.

Here is the code I'm using right now:

Code: Select all
 ProfilerApplicationCore application = new ProfilerApplicationCore();

            // Load the session.
            using (ProfilerSession session = application.LoadSession(@"Path to my session"))
            {
                SnapshotHeader[] snapshots = session.SessionFile.GetSnapshotHeaders();

                SnapshotHeader selectedSnapshot = snapshots[snapshots.Length - 1];
                SnapshotHeader comparisonSnapshot = snapshots[snapshots.Length - 2];

                using (ProfilerComparison comparison = application.CompareSnapshots(
                   selectedSnapshot, comparisonSnapshot))
                {
                    IgnoredIssuesCollection ignoredIssues = new IgnoredIssuesCollection();
                    ignoredIssues.Add(new IgnoredIssue(InstanceIssueIds.DuplicateInstance));

                    // Analyse the comparison to get information about any issues.
                    AnalysisResult analysisResult = comparison.AnalyseComparison(
                        new AnalysisSettings { IgnoreRuntimeIssues = true }, // Ignore runtime issues
                        new IgnoredIssuesCollection[] { ignoredIssues },
                        null // No pre-allocated AnalysisResult
                        );

                    var comparedTypes = analysisResult.Comparison.GetComparedTypes();
                    foreach (var comparedType in comparedTypes)
                    {
                        Console.WriteLine("HeldBytesCount " + comparedType.FullName + ": " + comparedType.HeldBytesCount);
                    }
                }
            }


So I guess something is still wrong.
Thanks for your help.

Best regards
inquistor
 
Posts: 3
Joined: Wed Jul 13, 2016 2:38 pm

Re: Getting CalculateHeldBytes in code

Postby Andreas Suurkuusk » Sun Jul 24, 2016 8:04 pm

I'm sorry for the delay. You are apparently using .NET Memory Profiler 4.6 or earlier, and therefore I had to investigate this a bit further. I'm currently on vacation, so I did not have access to the source of version 4.6 and has not been able investigate this until now.

In version 4.6, the held instances and bytes are not automatically updated by the analyser. In order to update the held bytes values you need to call the ProfilerComparison.UpdateComparedHeldSizes method. Add the following line after the call to AnalyseComparison:

Code: Select all
comparison.UpdateComparedHeldSizes();


I hope this helps.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 964
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden

Re: Getting CalculateHeldBytes in code

Postby inquistor » Tue Jul 26, 2016 10:50 am

Thank you, works like a charm now :)
inquistor
 
Posts: 3
Joined: Wed Jul 13, 2016 2:38 pm


Return to Using .NET Memory Profiler

Who is online

Users browsing this forum: No registered users and 3 guests

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron