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 feature 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos