Represents the base class for all memory assertion attributes.

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

Syntax

C#
public abstract class AssertionAttribute : Attribute
Visual Basic
Public MustInherit Class AssertionAttribute _
	Inherits Attribute
Visual C++
public ref class AssertionAttribute abstract : public Attribute

Remarks

The assertion attributes can be applied to any method and are used to declaratively define the expected memory usage of the method.

When running the method under .NET Memory Profiler, the profiler will insert memory assertions that will assert the expected memory usage automatically.

The assertion attributes map to the methods in the AssertionsDefinition class.

The following memory assertion attribute classes are available:

Examples

The following example shows a method that loads a bitmap using a dialog.

Copy CodeC#
using System.Drawing;
using SciTech.NetMemProfiler;

[NoNewInstances(“System.Windows.Forms.*”, IncludeSubclasses=true)]
[NoNewInstances(“System.Drawing.*”, IncludeSubclasses=true)]
[MaxNewInstances( typeof( Bitmap), 1] 
Bitmap LoadBitmap()
{
    using( LoadBitmapDialog dlg = new LoadBitmapDialog() )
    {
        dlg.ShowDialog();
        return dlg.LoadedBitmap;
    }
}

The applied attributes indicate that:

  • When the method returns no new instances of any type in the System.Windows.Forms namespace has been created, including all types derived from a type in the System.Windows.Forms namespace.
  • When the method returns no new instances of any type in the System.Drawing namespace (including all derived types) has been created, except...
  • ... a single instance of the Bitmap class.

When running under .NET Memory Profiler, the LoadBitmap method will be replaced with the following method:

Copy CodeC#
using System.Drawing;
using SciTech.NetMemProfiler;

Bitmap LoadBitmap()
{
    Bitmap returnValue;

    // --- Generated by .NET Memory Profiler
    // Collect a reference snapshot
    MemSnapShot refSnapshot = MemProfiler.FastSnapshot();

    try
    {
        // --- Original code
        using( LoadBitmapDialog dlg = new LoadBitmapDialog() )
        {
            dlg.ShowDialog();
            return dlg.LoadedBitmap;
        }
    // --- Generated by .NET Memory Profiler
    }
    finally
    {
        // Build the AssertionsDefinition, as 
        // defined by the attributes.
        using( MemAssertion.BeginAssertions() )
        {
            AssertionsDefinition ad = new AssertionsDefinition();
            ad.NoNewInstances( “System.Windows.Forms.*”, true );    
            ad.NoNewInstances( “System. Drawing.*”, true );        
            ad.MaxNewInstances( typeof( Bitmap ), 1 );            
            MemAssertion.Assert( refSnapshot, ad );
        }
    }
}

Inheritance Hierarchy

Thread Safety

Static members of this type are safe for multi-threaded operations. Instance members of this type are safe for multi-threaded operations.

See Also