Getting CalculateHeldBytes in code
Getting CalculateHeldBytes in code
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
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
-
- Posts: 1030
- Joined: Wed Mar 02, 2005 7:53 pm
Re: Getting CalculateHeldBytes in code
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).
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.
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
);
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
SciTech Software AB
Re: Getting CalculateHeldBytes in code
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:
So I guess something is still wrong.
Thanks for your help.
Best regards
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);
}
}
}
Thanks for your help.
Best regards
-
- Posts: 1030
- Joined: Wed Mar 02, 2005 7:53 pm
Re: Getting CalculateHeldBytes in code
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:
I hope this helps.
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
SciTech Software AB
Re: Getting CalculateHeldBytes in code
Thank you, works like a charm now 

Who is online
Users browsing this forum: No registered users and 14 guests