Page 1 of 1

API Ability to "query" snapshots?

Posted: Fri Nov 18, 2011 4:47 pm
by kellyconway
Is there any chance that we may be able to do something like this at some point using the API?

Code: Select all

MemSnapshot snapshotInitial = MemProfiler.FullSnapShot("Initial");


MemSnapshot snapshotFinal = MemProfiler.FullSnapShot("Final");

// The parts that would not work today follow...

if (snapshotFinal.TotalLiveBytes - snapsnot.Initial.TotalLiveBytes > someNumberOfBytes)
    // report potential memory leak

// OR maybe

if (snapshotFinal.DeltaLiveBytes > someNumberOfBytes)
    // report potential memory leak
IOW, will we at some point be able to "query" snapshots for info?

Also food for thought...

Code: Select all

int instancesInMemory = snapshotFinal.LiveInstances("My.Namespace.And.Type");

Re: API Ability to "query" snapshots?

Posted: Tue Nov 22, 2011 8:39 am
by Andreas Suurkuusk
At the moment there's no possibility to query snapshots using the in-process profiler API (using the MemProfiler and MemAssertion classes).

However, if you use the AssertionsDefinition class you can use the MaxNewBytes or MaxNewInstances assertion methods to assert that the additional memory usage is below a treshold.

So for your first example you could use:

Code: Select all

using( MemAssertion.BeginAssertions() )
  AssertionsDefinition ad = new AssertionsDefinition();
  ad.MaxNewBytes( TypeSet.All, someNumberOfBytes );

  MemAssertion.Assert( snapshot.Initial, ad );
(Using the MemAssertion.BeginAssertions method is not required, but it can avoid side effects and improve performance.)

As a sidenote, I recommend that you use the AssertionsDefinition class and the MemAssertion.Assert method when performing your assertions, instead of the other MemAssertion assertion methods. The AssertionDefinitions class provides more control over the assertions and have better performance if more than one assertion is performed.

Still, even if you can use the MaxNewBytes and MaxNewInstances assertions, actual information from the snapshots can of course also be useful. As this information is easily available for the profiler, we will probably add a few query properties and methods in a future version of the profiler.

Thanks for the suggestion.