Class AssertionsDefinition
Defines the expected memory usage of operations performed between two heap snapshots.
Inheritance
Implements
Inherited Members
Namespace: SciTech.NetMemProfiler
Assembly: SciTech.MemProfilerApi.dll
Syntax
public sealed class AssertionsDefinition : IDisposable
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.
MemAssertion.Assert
method.
The methods in the AssertionsDefinition class can be divided into three groups:
- NoXXX
This group contains the methods NoInstances
and NoNewInstances.
These methods are used to define that no instances should exist
of the specified types, unless the type has been specifically allowed
using an
AllowXXX
orMaxXXX
method. - AllowXXX
This group contains the methods AllowInstances
and AllowNewInstances.
These methods are used to define that instances of the specified types
are allowed to exist even if they have been restricted by a
call to a
NoXXX
method. Note that all types are allowed by default. If a type has not been restricted by aNoXXX
call, then allowing it usingAllowXXX
will have no effect. - MaxXXX
This group contains the methods MaxInstances,
MaxNewInstances, MaxBytes,
and MaxNewBytes.
These methods are used to define the maximum number of instances
or bytes that are allowed to exist at the time of the assertion.
They also override restrictions placed using a
NoXXX
call.
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:
AssertionsDefinition ad = new AssertionsDefinition();
ad.NoNewInstances( typeof( Control ), true );
ad.AllowNewInstances( typeof( TextBox ) );
and
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.
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
var baseSnapshot = 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( baseSnapshot, ad );
}
return loadedBitmap;
}
The code example below performs the same assertions as the previous example, but uses the MemAssertion.BeginAssertionsDefinition method and chained AssertionsDefinition calls to simplify the assertions.
/// <summary>
/// Tests the memory usage of the LoadBitmap method.
/// </summary>
Bitmap TestLoadBitmap()
{
// Establish a base snapshot
var baseSnapshot = 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( var ad = MemAssertion.BeginAssertionsDefinition( baseSnapshot ) )
{
ad.NoNewInstances( “System.Windows.Forms.*”, true )
.NoNewInstances( “System. Drawing.*”, true )
.MaxNewInstances( typeof( Bitmap ), 1 );
}
return loadedBitmap;
}
Constructors
Name | Description |
---|---|
AssertionsDefinition() | Initializes a new empty |
Methods
Name | Description |
---|---|
AllowInstances(TypeSet) | Defines that instances of any type in the provided |
AllowInstances(String) | Defines that instances of |
AllowInstances(String, Boolean) | Defines that instances of |
AllowInstances(String[]) | Defines that instances of |
AllowInstances(String[], Boolean) | Defines that instances of |
AllowInstances(Type) | Defines that instances of the provided |
AllowInstances(Type, Boolean) | Defines that instances of the provided |
AllowInstances(Type[]) | Defines that instances of the provided |
AllowInstances(Type[], Boolean) | Defines that instances of the provided |
AllowNewInstances(TypeSet) | Defines that new instances of any type in the provided |
AllowNewInstances(String) | Defines that new instances of |
AllowNewInstances(String, Boolean) | Defines that new instances of |
AllowNewInstances(String[]) | Defines that new instances of |
AllowNewInstances(String[], Boolean) | Defines that new instances of |
AllowNewInstances(Type) | Defines that new instances of the provided |
AllowNewInstances(Type, Boolean) | Defines that new instances of the provided |
AllowNewInstances(Type[]) | Defines that new instances of the provided |
AllowNewInstances(Type[], Boolean) | Defines that new instances of the provided |
Assert(MemSnapshot, Nullable<AssertionsThread>) | Asserts that the memory usage since the |
Assert(Nullable<AssertionsThread>) | Asserts that the memory usage since the |
Dispose() | If this instance was created using MemAssertion.BeginAssertionsDefinition this method will assert this definition (unless Assert has already been called) and then end the assertions session. |
MaxAllocatedBytes(TypeSet, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(String, Boolean, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(String, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(String[], Boolean, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(String[], Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(Type, Boolean, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(Type, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(Type[], Boolean, Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocatedBytes(Type[], Int64) | Defines that no more than the specified number of bytes are allowed to be allocated of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(TypeSet, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(String, Boolean, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(String, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(String[], Boolean, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(String[], Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(Type, Boolean, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(Type, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(Type[], Boolean, Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxAllocations(Type[], Int32) | Defines that no more than the specified number of allocations are allowed of the provided types when this definition is asserted using MemAssertion.Assert. |
MaxBytes(TypeSet, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances
of any type in the provided |
MaxBytes(String, Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
|
MaxBytes(String, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
|
MaxBytes(String[], Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
|
MaxBytes(String[], Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
|
MaxBytes(Type, Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
the provided |
MaxBytes(Type, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
the provided |
MaxBytes(Type[], Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
the provided |
MaxBytes(Type[], Int64) | Defines that no more than a specified number of bytes are allowed to be used by live instances of
the provided |
MaxInstances(TypeSet, Int32) | Defines that no more than a specified number of instances of any type in the
provided |
MaxInstances(String, Boolean, Int32) | Defines that no more than a specified number of instances of |
MaxInstances(String, Int32) | Defines that no more than a specified number of instances of |
MaxInstances(String[], Boolean, Int32) | Defines that no more than a specified number of instances of |
MaxInstances(String[], Int32) | Defines that no more than a specified number of instances of |
MaxInstances(Type, Boolean, Int32) | Defines that no more than a specified number of instances of the provided |
MaxInstances(Type, Int32) | Defines that no more than a specified number of instances of the provided |
MaxInstances(Type[], Boolean, Int32) | Defines that no more than a specified number of instances of the provided |
MaxInstances(Type[], Int32) | Defines that no more than a specified number of instances of the provided |
MaxNewBytes(TypeSet, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances
of any type in the provided |
MaxNewBytes(String, Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
|
MaxNewBytes(String, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
|
MaxNewBytes(String[], Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
|
MaxNewBytes(String[], Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
|
MaxNewBytes(Type, Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
the provided |
MaxNewBytes(Type, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
the provided |
MaxNewBytes(Type[], Boolean, Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
the provided |
MaxNewBytes(Type[], Int64) | Defines that no more than a specified number of bytes are allowed to be used by new live instances of
the provided |
MaxNewInstances(TypeSet, Int32) | Defines that no more than a specified number of new instances of any type in the
provided |
MaxNewInstances(String, Boolean, Int32) | Defines that no more than a specified number of new instances of |
MaxNewInstances(String, Int32) | Defines that no more than a specified number of new instances of |
MaxNewInstances(String[], Boolean, Int32) | Defines that no more than a specified number of new instances of |
MaxNewInstances(String[], Int32) | Defines that no more than a specified number of new instances of |
MaxNewInstances(Type, Boolean, Int32) | Defines that no more than a specified number of new instances of the provided |
MaxNewInstances(Type, Int32) | Defines that no more than a specified number of new instances of the provided |
MaxNewInstances(Type[], Boolean, Int32) | Defines that no more than a specified number of new instances of the provided |
MaxNewInstances(Type[], Int32) | Defines that no more than a specified number of new instances of the provided |
NoInstances(TypeSet) | Defines that no instances of any type in the provided |
NoInstances(String) | Defines that no instances of the |
NoInstances(String, Boolean) | Defines that no instances of the |
NoInstances(String[]) | Defines that no instances of |
NoInstances(String[], Boolean) | Defines that no instances of |
NoInstances(Type) | Defines that no instances of the provided |
NoInstances(Type, Boolean) | Defines that no instances of the provided |
NoInstances(Type[]) | Defines that no instances of the provided |
NoInstances(Type[], Boolean) | Defines that no instances of the provided |
NoNewInstances() | Defines that no new instances should exist when this definition is asserted using
|
NoNewInstances(TypeSet) | Defines that no new instances of any type in the provided |
NoNewInstances(String) | Defines that no new instances of the |
NoNewInstances(String, Boolean) | Defines that no new instances of the |
NoNewInstances(String[]) | Defines that no new instances of |
NoNewInstances(String[], Boolean) | Defines that no new instances of |
NoNewInstances(Type) | Defines that no new instances of the provided |
NoNewInstances(Type, Boolean) | Defines that no new instances of the provided |
NoNewInstances(Type[]) | Defines that no new instances of the provided |
NoNewInstances(Type[], Boolean) | Defines that no new instances of the provided |