dotMemory Unit 3.1 Help

Complex Scenarios. Traffic Class

In dotMemory Unit, memory traffic is represented with the Traffic type. The object of the Traffic type is returned as a result of the Memory.GetTrafficFrom* method.

To slice and dice traffic data, you should use a lambda expression passed to the Where method of the Traffic type. The Where method also returns an instance of the Traffic type allowing you to build chains of Where methods.

The Traffic type allows you to get data about allocated and collected memory using two properties: AllocatedMemory and CollectedMemory. These properties, in turn, have the ObjectsCount and SizeInBytes properties that you can use in test assertions.


For example, to make the assertion that the bar.Foo() method allocates no more than 1000 objects of the string type, write the following:

[DotMemoryUnit(CollectAllocations=true)] // collect traffic data [Test] public void TestMethod1() { var checkPoint1 = dotMemory.Check(); bar.Foo(); dotMemory.Check(memory => { Assert.That(memory.GetTrafficFrom(checkPoint1).Where(traffic => traffic.Type.Is<string>()) .AllocatedMemory.ObjectsCount, Is.LessThan(1000)); }); }

dotMemory Unit also provides alternative syntax for traffic queries. You can use the ==, &, and | logic operators to combine a number of queries. For example:

Assert.That(memory.GetTrafficFrom(memoryCheckPoint2).Where(obj => obj.Type == typeof(Foo) | obj.Interface == typeof(IEnumerable)) .AllocatedMemory.ObjectsCount, Is.LessThan(10));

Another benefit of using the Traffic class is the ability to group traffic by objects type:

dotMemory.Check(memory => { var traffic = memory.GetTrafficFrom(checkPoint1); var group = traffic.GroupByType(); var heavyTrafficTypes = group.Where(typeMemoryInfo => typeMemoryInfo.AllocatedMemoryInfo.SizeInBytes > 1000000).ToList(); // do sth. with heavyTrafficTypes });

Traffic class

Traffic methods

Where(Func<TrafficProperty, Query> query): TrafficGets memory traffic data by a specific condition. The condition is defined by a Query that should be returned by a lambda expression. The TrafficProperty instance passed to the lambda allows creating queries that filter traffic data by object type, interface, and other parameters.
Returns an instance of the Traffic type.
Where(TrafficQuery predicate): TrafficGets memory traffic data by a specific condition. The condition is defined by a reusable query - an instance of the TrafficPredicateQuery type. See QueryBuilder for details.
Returns an instance of the Traffic type.
GroupByType: IReadOnlyCollection<TypeTrafficInfo>Returns a collection of objects of the TypeTrafficInfo type. Each object represents traffic of objects of a particular type and carries info about the allocated and collected objects of that type.

Traffic properties

AllocatedMemoryMemoryInfoData on allocated objects: objects count and total size.
CollectedMemoryMemoryInfoData on collected objects: objects count and total size.

MemoryInfo struct

The MemoryInfo struct is used to get data about number of objects and their size.

MemoryInfo properties

ObjectsCountintTotal number of objects in the set.
SizeInByteslongTotal size of objects in the set.

TrafficProperty class

Filters traffic by a specific condition: type, interface, and other.

TrafficProperty properties

TypeTypePropertyAllows filtering traffic by object type.
InterfaceInterfacePropertyAllows filtering traffic by interface.
NamespaceNamespacePropertyAllows filtering traffic by namespace.
AssemblyAssemblyPropertyAllows filtering traffic by assemblies.

TypeTrafficInfo class

TypeTrafficInfo carries info about traffic of objects of the same type: allocated and collected objects. Instances of the TypeTrafficInfo class are a result of grouping the traffic via the GroupByType() method.

TypeTrafficInfo properties

AllocatedMemoryInfoMemoryInfoData on allocated objects of the Type.
CollectedMemoryInfoMemoryInfoData on collected objects of the Type.
TypeFullyQualifiedNamestringFully qualified name of the Type.
TypeTypeType of objects represented by this instance of the TypeTrafficInfo class.
Can be null in case the type is already unloaded by CLR or the type is in another AppDomain.
Last modified: 08 March 2021