Defines the expected memory usage of operations performed between two heap snapshots.

Namespace:  SciTech.NetMemProfiler
Assembly:  MemProfiler2 (in MemProfiler2.dll)

Syntax

C#
public sealed class AssertionsDefinition
Visual Basic
Public NotInheritable Class AssertionsDefinition
Visual C++
public ref class AssertionsDefinition sealed

Remarks

The AssertionsDefinition class provides the possibility to define the expected memory usage in multiple steps.

The expected memory usage is defined by creating an instance of this class and then building the definition using the methods in the class. When an assertions definition has been built, it can be asserted using the MemAssertion.Assert(MemSnapShot, AssertionsDefinition, AssertionsThread) method.

The methods in the AssertionsDefinition class can be divided into three groups:

Note Note The order of the calls to the AssertionsDefinition methods is not relevant; an AllowXXX or MaxXXX call always takes precedence over a NoXXX call. Consider the following two code snippets:
Copy CodeC#
AssertionsDefinition ad = new AssertionsDefinition();
ad.NoNewInstances( typeof( Control ), true );
ad.AllowNewInstances( typeof( TextBox ) );

and

Copy CodeC#
AssertionsDefinition ad = new AssertionsDefinition();
ad.AllowNewInstances( typeof( TextBox ) );
ad.NoNewInstances( typeof( Control ), true );

The above definitions are equal. An assertion using these definitions will fail if any new instance of a class derived from Control, except if the class is TextBox.

Examples

The code example below shows how a memory assertion can be performed using an AssertionsDefinition.
Copy CodeC#
using System.Drawing;
using SciTech.NetMemProfiler;

/// <summary>
/// This method opens up a form that allows the user
/// select a bitmap. The selected bitmap is loaded 
/// and made available through the LoadedBitmap 
/// property.
/// </summary>
Bitmap LoadBitmap()
{
    using( LoadBitmapDialog dlg = new LoadBitmapDialog() )
    {
        dlg.ShowDialog();
        return dlg.LoadedBitmap;
    }
}

/// <summary>
/// Tests the memory usage of the LoadBitmap method.
/// </summary> 
Bitmap TestLoadBitmap()
{
    // Establish a base snapshot
    MemProfiler.FastSnapshot();

    Bitmap loadedBitmap = LoadBitmap();

    // Assert that no new instances related to the 
    // System.Windows.Forms or System.Drawing
    // namespaces have been created, except 
    // for a single Bitmap instance.
    using( MemAssertion.BeginAssertions() )
    {
        AssertionsDefinition ad = new AssertionsDefinition();
        // No new instances of any type in the
        // System.Windows.Forms namespace should
        // exist, including all types derived from
        // a System.Windows.Forms type.
        ad.NoNewInstances( “System.Windows.Forms.*”, true );
        // No new instances of any type in the
        // System.Drawing namespace should
        // exist, including all types derived from
        // a System.Drawing type.
        ad.NoNewInstances( “System. Drawing.*”, true );        
        // Since a Bitmap is returned, we must allow one
        // new Bitmap to be created. The MaxNewInstances
        // assertion will override the NoNewInstances
        // assertion above.
        ad.MaxNewInstances( typeof( Bitmap ), 1 );            

        // The AssertionsDefinition has been built,
        // let’s perform the actual assertion.
        MemAssertion.Assert( ad );
    }

    return loadedBitmap;
}

Inheritance Hierarchy

System..::..Object
  SciTech.NetMemProfiler..::..AssertionsDefinition

See Also