Joydip Kanjilal
Contributor

Working with the DotMemory Unit framework

opinion
Mar 27, 20173 mins
Software Development

The DotMemory Unit framework from JetBrains can be used to analyze memory usage, write unit tests, and detect memory issues in .Net apps

dotmemory
Credit: IDG

Unit testing helps to detect bugs and reduce time to market. It is the act of testing small components or units of code so as to find, reduce, or eliminate errors in your applications.

I recently explored JetBrains’ DotMemory Unit framework, which extends the functionalities provided by the popular unit testing frameworks together with the functionalities of a memory profiler.

Why DotMemory Unit? Why is it helpful?

The DotMemory Unit framework enables you to write unit tests that can check your code to determine memory issues, i.e., memory leaks and memory traffic. It’s a unit testing framework together with the features of a memory profiler: It can check for memory allocation of a particular type, check for memory traffic, and get memory snapshots for additional investigation on memory issues.

Most importantly, this framework is available for free and is also distributed as a NuGet package or a standalone launcher. It’s compatible with such popular unit testing as MSTest, NUnit, and xUnit.net. Note that if you’re using a unit testing framework that’s not in the list of the supported frameworks, you can still use DotMemory Unit by leveraging the DotMemoryUnitController class. You can learn more on this from here.

Getting started

You can install DotMemory Unit either as a NuGet package via the NuGet Package Manager or also as a standalone installer. You can install it by running the Install-Package JetBrains.DotMemoryUnit command in the Package Manager Console. You can also download the freely available standalone installer.

Note that to run unit tests using DotMemory Unit, you should have either dotCover or ReSharper installed in your system. If ReSharper is installed and you have installed DotMemory Unit package, you should see the “Run Unit Tests under DotMemory Unit” option under the ReSharper menu for unit tests.

You can also run your unit tests using the DotMemory Unit standalone unit test runner in lieu of the Visual Studio IDE. You can learn more on this here.

Using the DotMemory Unit testing framework

The following code snippet illustrates how you can check for count of objects of a specific type using the DotMemory Unit framework:

[TestMethod]

public void CheckObjectsTest()

 {

    DotMemory.Check(memory =>

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

 }

If you were to check if the number of objects in Generation 2 is greater than a particular threshold value (say 5), you can write the following unit test method.

[TestMethod]

[DotMemoryUnit(CollectAllocations = true)]

public void CheckObjectsTest()

 {

   DotMemory.Check(memory =>

   Assert.IsTrue(memory.GetObjects(where => where.Generation.Is(Generation.LOH)).ObjectsCount > 5));

 }

You can also check for memory traffic using the AssertTraffic attribute. As an example, you can check for memory traffic of objects belonging to a particular type (say, Employee). It should be noted that collection of memory allocation data is not turned on by default. You would need to explicitly turn it on using the DotMemoryUnit attribute in your test method.

 [DotMemoryUnit(CollectAllocations = true)]

        [TestMethod]

        public void CheckMemoryTrafficTest()

        {

            var memoryCheckPoint1 = DotMemory.Check();

            Employee emp = new Employee();

            DotMemory.Check(

                    memory =>

                        Assert.IsTrue(

                            memory.GetTrafficFrom(memoryCheckPoint1)

                                .Where(obj => obj.Type.Is<Employee>())

                                .AllocatedMemory.ObjectsCount <= 1));

        }

Refer to the test method given above. Since one instance of the Employee class has been created here, the test passes. Note how checkpoints have been used to mark the time intervals where memory traffic needs to be analysed. The GetTrafficFrom method is used to get memory traffic data within a particular interval of time. Essentially, the DotMemory.Check method takes a memory snapshot that you can use later to analyze memory traffic.

Joydip Kanjilal
Contributor

Joydip Kanjilal is a Microsoft Most Valuable Professional (MVP) in ASP.NET, as well as a speaker and the author of several books and articles. He received the prestigious MVP award for 2007, 2008, 2009, 2010, 2011, and 2012.

He has more than 20 years of experience in IT, with more than 16 years in Microsoft .Net and related technologies. He has been selected as MSDN Featured Developer of the Fortnight (MSDN) and as Community Credit Winner several times.

He is the author of eight books and more than 500 articles. Many of his articles have been featured at Microsoft’s Official Site on ASP.Net.

He was a speaker at the Spark IT 2010 event and at the Dr. Dobb’s Conference 2014 in Bangalore. He has also worked as a judge for the Jolt Awards at Dr. Dobb's Journal. He is a regular speaker at the SSWUG Virtual Conference, which is held twice each year.

More from this author