The DotMemory Unit framework from JetBrains can be used to analyze memory usage, write unit tests, and detect memory issues in .Net apps 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. Related content how-to How to use FastEndpoints in ASP.NET Core Take advantage of the free open-source FastEndpoints library to build fast and lean APIs in your ASP.NET Core applications. By Joydip Kanjilal Jul 11, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Refit to consume APIs in ASP.NET Core Take advantage of Refit REST library to simplify API consumption and make your code cleaner, more efficient, and easier to maintain. By Joydip Kanjilal Jul 04, 2024 10 mins C# Microsoft .NET Software Deployment how-to When to use an abstract class vs. interface in C# Understanding the differences between an abstract class and interface is key to designing loosely coupled and extensible applications. By Joydip Kanjilal Jun 20, 2024 10 mins Small and Medium Business Microsoft .NET C# how-to 6 security best practices for ASP.NET Core Learn the best practices and built-in safeguards for preventing attacks and protecting sensitive data in your ASP.NET Core web applications. By Joydip Kanjilal Jun 07, 2024 6 mins C# Microsoft .NET Web Development Resources Videos