dotMemory Unit 3.1 Help


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 dotMemory Unit 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).

  • To run tests with dotMemory Unit support, you can use 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 (available in both NuGet package and the zip file). 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 ignored.

  • dotMemory Unit works with almost all of the unit-testing frameworks on the market including MSTest, NUnit, and

How does it look

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. 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.

Snapshot compatibility

Save inOpen in
dotMemory 10 – 2017.1dotMemory 2017.2dotMemory 2017.3 – 2019.1
dotMemory Unit 1.0 – 2.3+--
dotMemory Unit 2.4-++
dotMemory Unit 3.0 – 3.1--+
Last modified: 08 March 2021