SciTech.NetMemProfiler.Core: Save snapshot for later use

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

SciTech.NetMemProfiler.Core: Save snapshot for later use

Post by JuergenKlopf » Mon Nov 19, 2012 9:09 am

Hello,

I need to compare snapshots from 2 different runs of the memory profiler. Is there any possibilty to save and reload the (primary) snapshot with the API of the MemoryProfiler?

Thanks in advanced!

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

Re: SciTech.NetMemProfiler.Core: Save snapshot for later use

Post by Andreas Suurkuusk » Mon Nov 19, 2012 11:34 pm

To compare snapshots from two different session files, you can use the ProfilerApplicationCore.CompareSnapshots method that accepts two SnapshotHeaders. Below is a sample that compares the first snapshot in two different session files.

Code: Select all

using System;
using System.Collections.Generic;
using SciTech.Profiler;
using SciTech.Profiler.Memory;

namespace ProfilerAPI
{
    class Program
    {
        private static void Main(string[] args)
        {
            ProfilerApplicationCore app = new ProfilerApplicationCore();

            using (var session1 = app.LoadSession(@"<path to primary session>"))
            {
                using (var session2 = app.LoadSession(@"<path to comparison session>"))
                {
                    SnapshotHeader[] headers1 = session1.SessionFile.GetSnapshotHeaders();
                    SnapshotHeader[] headers2 = session2.SessionFile.GetSnapshotHeaders();

                    // Make sure that both sessions contain at least one snapshot
                    if( headers1.Length > 0 && headers2.Length > 0 )
                    {
                        // Compare the first snapshot from both sessions
                        using (ProfilerComparison comparison = app.CompareSnapshots(
                            headers1[0], headers2[0]))
                        {
                            var comparedTypes = comparison.GetComparedTypes();

                            // Print delta information for all types with
                            // delta <> 0
                            foreach (var comparedType in comparedTypes)
                            {
                                if (comparedType.DeltaLiveBytesCount != 0)
                                {
                                    Console.WriteLine(
                                        comparedType 
                                        + " - delta bytes: "
                                        + comparedType.DeltaLiveBytesCount);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
You can also compare a snapshot from an active session with a comparison snapshot from a saved session. However, the active session snapshot must be the primary snapshot, and you must use the ProfilerComparison instance associated with the active session.

Code: Select all

private static void Main(string[] args)
{
    ProfilerApplicationCore app = new ProfilerApplicationCore();

    using (ProfilerSession savedSession = app.LoadSession(@"<path to saved session>"))
    {
        SnapshotHeader[] saveHeaders = savedSession.SessionFile.GetSnapshotHeaders();
        ProfileProcessStartInfo startInfo = new ProfileProcessStartInfo(
            ProfilingType.Application,
            @"<path to application>");
        using (ActiveSession activeSession = app.CreateSession(startInfo))
        {
            // Start the profiling.                
            activeSession.Start( TimeSpan.FromMinutes( 1 ) );

            // Wait for a while.
            Thread.Sleep( TimeSpan.FromSeconds( 20 ) );

            // And collect a snapshot.
            // NOTE! The "internal" API (MemProfiler.FullSnapShot) is probably more suitable
            // for collecting snapshots.
            activeSession.CollectSnapshot( SnapshotCollection.FullGC );

            SnapshotHeader[] activeHeaders = activeSession.SessionFile.GetSnapshotHeaders();

            // Compare the first snapshot from both sessions
            // Must use the ProfilerComparison of the active session
            // (cannot use ProfilerApplicationCore.CompareSnapshots, 
            // as that creates a new ProfilerComparison).
            var comparison = activeSession.Comparison;
            comparison.CompareSnapshots( 
                activeHeaders[0], 
                saveHeaders[0] );
                    
            var comparedTypes = comparison.GetComparedTypes();

            // Print delta information for all types with
            // delta <> 0
            foreach (var comparedType in comparedTypes)
            {
                if (comparedType.DeltaLiveBytesCount != 0)
                {
                    Console.WriteLine(
                        comparedType 
                        + " - delta bytes: "
                        + comparedType.DeltaLiveBytesCount);
                }
            }

            // Stop the active session and terminate application
            activeSession.Stop();
        }
    }
}
If you want to save a session file using the API, you can just call ActiveSession.Save, preferably after the session has been stopped.

Code: Select all

// Save session; with all snapshots and real-time data
activeSession.Save(@"<path>", null, true);
Best regards,

Andreas Suurkuusk
SciTech Software AB

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

Re: SciTech.NetMemProfiler.Core: Save snapshot for later use

Post by JuergenKlopf » Wed Nov 28, 2012 2:06 pm

Thank you very much, that is exactly what I need!

One more question: For first monitoring I want to get the delta of instances between the primary and comparison snapshot. Could you provide me the way to get these differences?

Thank you very much!

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

Re: SciTech.NetMemProfiler.Core: Save snapshot for later use

Post by Andreas Suurkuusk » Wed Nov 28, 2012 9:41 pm

To get the delta instances of a type between the primary and comparison snapshot, you can use the ComparedTypeInfo.DeltaLiveInstancesCount property (instead of DeltaLiveBytesCount). The ComparedTypeInfo class contains properties for all columns presented under the Overview page in .NET Memory Profiler (even though many properties will be null when you compare snapshots from two different sessions). And even if the API documentation is not (yet) available, you can get information about the available methods and properties using Visual Studio Intellisense. If you need additional information about any methods or properties, you can just post a question on this forum.
Best regards,

Andreas Suurkuusk
SciTech Software AB

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 22 guests