Search Results for

    Show / Hide Table of Contents

    Class AssertionsDefinition

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

    Inheritance
    System.Object
    AssertionsDefinition
    Implements
    System.IDisposable
    Inherited Members
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.ToString()
    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 or MaxXXX 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 a NoXXX call, then allowing it using AllowXXX 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 AssertionsDefinition instance.

    Methods

    Name Description
    AllowInstances(TypeSet)

    Defines that instances of any type in the provided typeSet are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(String)

    Defines that instances of Types that matches the provided name are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(String, Boolean)

    Defines that instances of Types that matches the provided name (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(String[])

    Defines that instances of Types that matches the provided names are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(String[], Boolean)

    Defines that instances of Types that matches the provided names (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(Type)

    Defines that instances of the provided type are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(Type, Boolean)

    Defines that instances of the provided type (and optionally its subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(Type[])

    Defines that instances of the provided types are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowInstances(Type[], Boolean)

    Defines that instances of the provided types (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(TypeSet)

    Defines that new instances of any type in the provided typeSet are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(String)

    Defines that new instances of Types that matches the provided name are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(String, Boolean)

    Defines that new instances of Types that matches the provided name (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(String[])

    Defines that new instances of Types that matches the provided names are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(String[], Boolean)

    Defines that new instances of Types that matches the provided names (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(Type)

    Defines that new instances of the provided type are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(Type, Boolean)

    Defines that new instances of the provided type (and optionally its subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(Type[])

    Defines that new instances of the provided types are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    AllowNewInstances(Type[], Boolean)

    Defines that new instances of the provided types (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    Assert(MemSnapshot, Nullable<AssertionsThread>)

    Asserts that the memory usage since the comparisonSnapshot meets the memory usage defined by this AssertionsDefinition. This is equivalent to calling the MemAssertion.Assert method.

    Assert(Nullable<AssertionsThread>)

    Asserts that the memory usage since the comparisonSnapshot meets the memory usage defined by this AssertionsDefinition. This is equivalent to calling the MemAssertion.Assert method.

    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 typeSet when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(String, Boolean, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of Types that matches the provided name (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(String, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of Types that matches the provided name when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(String[], Boolean, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of Types that matches the provided names (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(String[], Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of Types that matches the provided names when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 type (and optionally its subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(Type, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of the provided type when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 types (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxBytes(Type[], Int64)

    Defines that no more than a specified number of bytes are allowed to be used by live instances of the provided types when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(TypeSet, Int32)

    Defines that no more than a specified number of instances of any type in the provided typeSet are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(String, Boolean, Int32)

    Defines that no more than a specified number of instances of Types that matches the provided name (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(String, Int32)

    Defines that no more than a specified number of instances of Types that matches the provided name are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(String[], Boolean, Int32)

    Defines that no more than a specified number of instances of Types that matches the provided names (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(String[], Int32)

    Defines that no more than a specified number of instances of Types that matches the provided names are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(Type, Boolean, Int32)

    Defines that no more than a specified number of instances of the provided type (and optionally its subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(Type, Int32)

    Defines that no more than a specified number of instances of the provided type are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(Type[], Boolean, Int32)

    Defines that no more than a specified number of instances of the provided types (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxInstances(Type[], Int32)

    Defines that no more than a specified number of instances of the provided types are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 typeSet when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewBytes(String, Boolean, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by new live instances of Types that matches the provided name (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewBytes(String, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by new live instances of Types that matches the provided name when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewBytes(String[], Boolean, Int64)

    Defines that no more than a specified number of bytes are allowed to be used by new live instances of Types that matches the provided names (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewBytes(String[], Int64)

    Defines that no more than a specified number of bytes are allowed to be used by new live instances of Types that matches the provided names when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 type (and optionally its subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 type when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 types (and optionally their subclasses) when this definition is asserted using MemAssertion.MemAssertion.Assert.

    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 types when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(TypeSet, Int32)

    Defines that no more than a specified number of new instances of any type in the provided typeSet are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(String, Boolean, Int32)

    Defines that no more than a specified number of new instances of Types that matches the provided name (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(String, Int32)

    Defines that no more than a specified number of new instances of Types that matches the provided name are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(String[], Boolean, Int32)

    Defines that no more than a specified number of new instances of Types that matches the provided names (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(String[], Int32)

    Defines that no more than a specified number of new instances of Types that matches the provided names are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(Type, Boolean, Int32)

    Defines that no more than a specified number of new instances of the provided type (and optionally its subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(Type, Int32)

    Defines that no more than a specified number of new instances of the provided type are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(Type[], Boolean, Int32)

    Defines that no more than a specified number of new instances of the provided types (and optionally their subclasses) are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    MaxNewInstances(Type[], Int32)

    Defines that no more than a specified number of new instances of the provided types are allowed to exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(TypeSet)

    Defines that no instances of any type in the provided typeSet should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(String)

    Defines that no instances of the Types that matches the provided name (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(String, Boolean)

    Defines that no instances of the Types that matches the provided name (and optionally its subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(String[])

    Defines that no instances of Types that matches the provided names should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(String[], Boolean)

    Defines that no instances of Types that matches the provided names (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(Type)

    Defines that no instances of the provided type should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(Type, Boolean)

    Defines that no instances of the provided type (and optionally its subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(Type[])

    Defines that no instances of the provided types should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoInstances(Type[], Boolean)

    Defines that no instances of the provided types (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances()

    Defines that no new instances should exist when this definition is asserted using MemAssertion.xref:SciTech.NetMemProfiler.MemAssertion.Assert*.

    NoNewInstances(TypeSet)

    Defines that no new instances of any type in the provided typeSet should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(String)

    Defines that no new instances of the Types that matches the provided name should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(String, Boolean)

    Defines that no new instances of the Types that matches the provided name (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(String[])

    Defines that no new instances of Types that matches the provided names should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(String[], Boolean)

    Defines that no new instances of Types that matches the provided names (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(Type)

    Defines that no new instances of the provided type should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(Type, Boolean)

    Defines that no new instances of the provided type (and optionally its subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(Type[])

    Defines that no new instances of the provided types should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    NoNewInstances(Type[], Boolean)

    Defines that no new instances of the provided types (and optionally their subclasses) should exist when this definition is asserted using MemAssertion.MemAssertion.Assert.

    In This Article
    Back to top

    © Copyright 2002-2020. SciTech Software AB.
    For information about .NET Memory Profiler, see the product site at https://memprofiler.com