dotMemory Unit 3.2 Help

Introduction

dotMemory Unit is an additional framework that pairs with popular unit testing frameworks. Its purpose is to let you 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 Visual Studio with ReSharper or JetBrains Rider) 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 Visual Studio with ReSharper, JetBrains dotCover, or JetBrains Rider 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 Use dotMemory Unit Standalone Launcher.

  • After you install the dotMemory Unit package, ReSharper's/Rider's menus for unit tests will get the additional Run Unit Tests under dotMemory Unit item. In this mode, test runner will execute dotMemory Unit framework calls as well as ordinary test logic. If you run a test in an "ordinary way" (without dotMemory Unit support), depending on the settings, either the test will fail or dotMemory Unit calls will be ignored.

  • dotMemory Unit works with almost all of the popular unit-testing frameworks including MSTest, NUnit, and xUnit.net.

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 ordinary unit tests:

  • 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, e.g., objects left in memory or significant memory traffic.

Getting started

  • The fastest way to start writing your own memory tests is to read Get Started.

  • If you look for test examples, refer to Examples.

  • For more information about particular framework methods, refer to Reference.

Snapshot compatibility

Save in

Open in

dotMemory 10 – 2017.1

dotMemory 2017.2

dotMemory 2017.3 – 2019.1

dotMemory Unit 1.0 – 2.3

+

-

-

dotMemory Unit 2.4

-

+

+

dotMemory Unit 3.0 – 3.2

-

-

+

Last modified: 23 November 2023