dotMemory Unit 3.0 Help

Working with Memory

To analyze memory, first you need to take a memory dump (in terms of dotMemory Unit, called memory checkpoint or snapshot). To take memory checkpoints, you should use the Check method of the static dotMemory type. Therefore, the dotMemory.Check is an essential starting point for almost any memory test.

A MemoryCheckPoint instance returned by dotMemory.Check can be used as a baseline for comparison with other checkpoints, or for checking memory traffic between checkpoints. See Comparing Snapshots and Working with Traffic correspondingly.

A lambda passed to the dotMemory.Check method allows slicing and dicing memory by specific conditions. An instance of the Memory type passed to that lambda contains all memory data at the current execution point. Use it to get data about specific objects and filter traffic data. See Working with Object Sets and Working with Traffic correspondingly.

Example

For instance, in the example below, the memory object of the Memory type passed to the dotMemory.Check method contains all memory data in the current execution point. A lambda passed to the memory.GetObjects method returns a set of objects by a certain condition (all objects of the Foo type).

dotMemory.Check(memory => { Assert.That(memory.GetObjects(where => where.Type.Is<Foo>()).ObjectsCount, Is.EqualTo(0)); });

In the next example, we create a memory checkpoint before running the user method Foo.Bar(), then (after the user method finishes working) we create the next checkpoint and assert that traffic between checkpoints haven't exceeded 1000 bytes.

var checkPoint1 = dotMemory.Check(); // create memory checkpoint #1 foo.Bar(); // run user method // Assert that foo.Bar() allocates no more than 1000 bytes dotMemory.Check(memory => { Assert.That(memory.GetTrafficFrom(memoryCheckPoint1).AllocatedMemory.SizeInBytes, Is.LessThanOrEqualTo(1000)); });

MemoryCheckPoint struct

Represents a reference to memory data in some execution point. Use this reference to compare data taken in different execution points or get data about memory traffic between two points. An instance of the MemoryCheckPoint type is returned by the dotMemory.Check method.

dotMemory static class

dotMemory Methods

NameDescription
Check(): MemoryCheckPoint Returns an instance of the MemoryCheckPoint type which is a reference to memory data in the current execution point. Use this reference as a base for comparison with memory data taken in other points.
Check(Action<Memory> check): MemoryCheckPoint Returns a reference to memory data in the current execution point and allows checking these data using Assert statements. The check Action is a lambda expression that should contain the assertions. The Memory instance passed to the lambda provides an interface for accessing the memory data. By default, if an assertion fails, dotMemory Unit will auto-save the workspace with all collected snapshots. Use this workspace to analyze why the test fails. Learn how to change the default auto-save behavior.

Memory class

Represents memory data in some execution point. Object of the Memory class is used to access memory data with the help of Assert statements. The statements are passed in a lambda expression to the dotMemory.Check method.

Memory methods

NameDescription
GetObjects(Func<ObjectProperty, Query> query): ObjectSet (Inherited from ObjectSet)
Gets a subset of objects by a specific condition. The condition is defined by query in the form of a lambda expression. The ObjectProperty object passed to the lambda allows selecting objects by type, interface, and other parameters.
Returns an instance of the ObjectSet type.
GroupByType: IReadOnlyCollection<TypeMemoryInfo> (Inherited from ObjectSet)
Returns a collection of objects of the TypeMemoryInfo type. Each object represents a particular type from the source object set and carries info about the number of objects of that type and their total size.
GetDifference(MemoryCheckPoint memoryCheckPoint): SnapshotDifference Gets difference between the current memory checkpoint and the checkpoint passed in the memoryCheckPoint parameter.
Returns an instance of the SnapshotDifference type that allows you to get data about new, dead, and survived objects.
GetTrafficFrom(MemoryCheckPoint memoryCheckPoint): Traffic Gets data about memory traffic in the time interval between the current memory snapshot and the snapshot passed in the memoryCheckPoint parameter.
Returns an instance of the Traffic type that allows you to get data about objects that were allocated and collected during the specified time interval.

Memory properties

NameTypeDescription
ObjectsCount int (Inherited from ObjectSet)
Total number of objects in memory.
SizeInBytes long (Inherited from ObjectSet)
Total size of objects in memory.
Last modified: 16 January 2018