(Professional only)
Memory assertions can also be performed declaratively by appending attributes to methods. This is a convenient way of expressing the expected memory behavior of a method, and when running under .NET Memory Profiler the expectations will be asserted automatically.
For each method in the AssertionsDefinition class, there exists a corresponding attribute in the SciTech.NetMemProfiler namespace, as can be seen in the following table.
|
AssertionsDefinition Method |
Corresponding Attribute |
|
AssertionsDefinition.NoNewInstances |
NoNewInstancesAttribute |
|
AssertionsDefinition.NoInstances |
NoInstancesAttribute |
|
AssertionsDefinition.AllowNewInstances |
AllowNewInstancesAttribute |
|
AssertionsDefinition.AllowInstances |
AllowInstancesAttribute |
|
AssertionsDefinition.MaxNewInstances |
MaxNewInstancesAttribute |
|
AssertionsDefinition.MaxInstances |
MaxInstancesAttribute |
|
AssertionsDefinition.MaxNewBytes |
MaxNewBytesAttribute |
|
AssertionsDefinition.MaxBytes |
MaxBytesAttribute |
If a method has one or more assertion attributes, the profiler will replace the method with a method that:
1. Collects a reference snapshot.
2. Executes the original code.
3. Create an AssertionsDefinition corresponding to the attributes.
4. Asserts that the expected memory behavior is met by calling MemAssertion.Assert.
For instance, instead of using the TestLoadBitmap method in the AssertionsDefinition example, it is possible to express the assertions using attributes like this:
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;
}
}
When running this code under .NET Memory Profiler, the profiler will replace this method with a method looking similar to this:
using System.Drawing;
using SciTech.NetMemProfiler;
Bitmap LoadBitmap()
{
// --- 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 );
}
}
}
As can be seen, the code in the replaced method is similar to the code in the AssertionsDefinition example. The advantage of using attributes is that the expected memory behavior is documented by the attributes, and there is no code overhead when not running under the profiler.
For more information about the assertion attributes, see the .NET Memory Profiler API Reference documentation.
|
.NET Memory Profiler User Manual © Copyright 2002-2013. SciTech Software AB. For information about .NET Memory Profiler, see the product site at http://memprofiler.com .NET Memory Profiler is developed by SciTech Software AB |