Search Results for

    Show / Hide Table of Contents

    Method BeginAssertions

    BeginAssertions()

    Begins an assertions session. An assertions session is used to reduce the number of garbage collections performed when doing memory assertions and to avoid memory side effects.

    Declaration
    public static MemAssertion.AssertionsSession BeginAssertions()
    Returns
    Type Description
    MemAssertion.AssertionsSession

    An AssertionsSession instance that can be used to end the assertions session.

    Remarks

    BeginAssertions should be used to reduce the number of garbage collections performed when doing multiple memory assertions at the same time.

    Normally each memory assertion makes two full garbage collections, and waits for pending finalizers. When BeginAssertions is called, the garbage collections are performed directly and no new garbage collections will be performed by memory assertions, until EndAssertions() is called.

    It is also recommended to call BeginAssertions/EndAssertions to avoid falsely identifying instances as memory leaks. This is especially important if using the NoNewInstancesExcept methods or if an AssertionsDefinition is created for the assertions. Consider the following assertion:

    var comparisonSnapshot = MemProfiler.FastSnapshot();
    

    // Do some operation that will be checked

    MemAssertion.NoNewInstancesExcept( comparisonSnapshot, new Type[] { typeof( SomeClass), typeof( SomeOtherClass ) );

    The problem with the assertion above is that at the time of the assertion call, a new Type array exists and the assertion will fail. If BeginAssertions/EndAssertions is used this will be avoided, since instances created in-between BeginAssertions and EndAssertions are ignored by the memory assertion.

    This method returns an MemAssertion.AssertionsSession instance. If Dispose is called on this instance, it will automatically call EndAssertions. This allows the assertions session to be ended using the "using" pattern. If the process is not being profiled, this methods does not nothing and the Dispose method of the returned AssertionsSession will also do nothing.

    var comparisonSnapshot = MemProfiler.FastSnapshot();
    

    // Do some operation that will be checked

    using( BeginAssertions() ) { MemAssertion.NoNewInstancesExcept( comparisonSnapshot, new Type[] { typeof( SomeClass), typeof( SomeOtherClass ) ); }

    See Also
    MemAssertion.BeginAssertionsDefinition
    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