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 What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos