dotMemory Unit 2.2 Help

Introduction

dotMemory Unit is a unit testing framework that allows you to write tests that check your code for all kinds of memory issues. For example, these can be tests that determine leaks by checking memory for objects of a particular type, or tests that track memory traffic and fail in case the traffic exceeds some threshold. In other words, dotMemory Unit extends the possibilities of your unit testing framework with the functionality of a memory profiler. More specifically, dotMemory Unit allows:

  • Checking memory for objects of a certain type
  • Checking memory traffic
  • Getting difference between memory snapshots
  • Saving memory snapshots for further investigation in dotMemory (standalone .NET memory profiler)

How This Works

  • dotMemory Unit is distributed as a NuGet package (convenient if you use the ReSharper unit test runner) or as a zip package (if you want to use a standalone dotMemory Unit launcher).
  • Tests with the support for dotMemory Unit can be run using the ReSharper unit test runner. In this case, you should have either ReSharper 9.1 (or later) or dotCover 3.1 (or later) installed on your machine. Another option is to run tests using the standalone dotMemory Unit launcher. You can take the launcher either from the NuGet package or from the zip available for download on the dotMemory Unit page. Learn how to use the launcher in Using DotMemory Unit Standalone Launcher.
  • After you install the dotMemory Unit package, ReSharper's menus for unit tests will get the additional item Run Unit Tests under dotMemory Unit. In this mode, test runner will execute dotMemory Unit framework calls as well as ordinary test logic. If you run a test 'normal' way (without dotMemory Unit support), depending on the settings the test will fail or dotMemory Unit calls will be simply ignored.
  • dotMemory Unit works with almost all of the unit-testing frameworks on the market including MSTest, NUnit, and xUnit.net.

How This Looks

To quickly introduce you to the framework, let’s consider the following test example.

[Test] public void TestMethod1() { ... // do some work dotMemory.Check(memory => Assert.That(memory.GetObjects(where => where.Type.Is<Foo>()).ObjectsCount, Is.EqualTo(0))); }

This NUnit test selects all objects of the Foo type from memory and asserts that there are no such objects. Such a test is quite common when you look for memory leaks in your code.

Note that dotMemory Unit does not force you to use any specific Assert syntax. Simply use the syntax of the framework your test is written for. For example, the shown assertion uses the NUnit syntax but could be easily updated for MSTest*:

Assert.AreEqual(0, memory.GetObjects(where => where.Type.Is<Foo>()).ObjectsCount);

When to use dotMemory Unit

Use "memory" tests in the same way as the unit tests on app logic:

  • After you manually find an issue (such as a leak), write a memory test that covers it.
  • Write tests for proactive testing — to ensure that new product features do not create any memory issues, like objects left in memory or large memory traffic.

Getting Started

The fastest way to start writing your own "memory" tests is to read Get Started. It contains information about installing dotMemory Unit and running "memory" tests. If you look for test examples, refer to the Examples section. For the details about certain framework methods, refer to the Reference section.

See Also

Last modified: 4 May 2016