Add filter for additional instances in code

Use this forum for questions on how to use .NET Memory Profiler and how to analyse memory usage.
Post Reply
JuergenKlopf
Posts: 6
Joined: Wed Jul 11, 2012 1:48 pm

Add filter for additional instances in code

Post by JuergenKlopf » Tue Feb 19, 2013 1:16 pm

I want to be able to get the analysis issues with the level for all types with additional instances (like it is possible in the stand alone tool):

Code: Select all

private void GetResult(ProfilerComparison comparison)
{
    var analysisResult = comparison.AnalyseComparison(
        new AnalysisSettings(), new[] { ignoredIssues }, null);
    var issueInstances = analysisResult.GetInstancesWithIssues(issueId => true);
    foreach (var instanceId in issueInstances)
    {
        var analysisIssues = analysisResult.GetInstanceIssues(instanceId);
        foreach (var analysisIssue in analysisIssues)
        {
            if (analysisIssue.Level == AnalysisInfoLevel.Warning 
                // && IssuesWithAdditionalInstances (delta > 0))
            {
                // something to do
            }
        }
    }
}
As you showed me in an earlier post http://forum.memprofiler.com/viewtopic.php?f=2&t=4299 it is possible to get the ComparedTypeInfo but I don't know how I can put these 2 things (AnalysisInfoLevel and ComparedTypeInfo) together. Would you be so kind to show me a way to achieve this?

Thanks in advanced!

Andreas Suurkuusk
Posts: 1029
Joined: Wed Mar 02, 2005 7:53 pm

Re: Add filter for additional instances in code

Post by Andreas Suurkuusk » Tue Feb 19, 2013 7:55 pm

To get the ComparedTypeInfo of a type, you can use the ProfilerComparison.GetComparedType(ManagedType) method. To get the type of an instance, you need to first get the TypeInstance using ProfilerComparison.GetTypeInstance(TypeInstanceId) and then you can use the ManagedType property. (The ManagedType class is closely related to the System.Type class and provides similar properties.)

E.g.

Code: Select all

TypeInstance instance = comparison.GetTypeInstance(instanceId);
ManagedType type = instance.ManagedType;
var comparedTypeInfo = comparison.GetComparedType(type);
Note that if you wish to get all issues at type level, you can use the method AnalysisResult.GetTypesWithIssues(). This method will return an array of types that has at least on issue (at instance or type level). You can then use the method AnalysisResult.GetTypeIssues(ManagedType).
Best regards,

Andreas Suurkuusk
SciTech Software AB

JuergenKlopf
Posts: 6
Joined: Wed Jul 11, 2012 1:48 pm

Re: Add filter for additional instances in code

Post by JuergenKlopf » Wed Feb 20, 2013 7:43 am

Your last hint is exactly what I need. Thanks again for your support!

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 13 guests