dotMemory Unit 3.2 Help

Complex Cases. 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.

Example

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

Name

Description

Where(Func<TrafficProperty, Query> query): Traffic

Gets 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): Traffic

Gets 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

Name

Type

Description

AllocatedMemory

MemoryInfo

Data on allocated objects: objects count and total size.

CollectedMemory

MemoryInfo

Data 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

Name

Type

Description

ObjectsCount

int

Total number of objects in the set.

SizeInBytes

long

Total size of objects in the set.

TrafficProperty class

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

TrafficProperty properties

Name

Type

Description

Type

TypeProperty

Allows filtering traffic by object type.

Interface

InterfaceProperty

Allows filtering traffic by interface.

Namespace

NamespaceProperty

Allows filtering traffic by namespace.

Assembly

AssemblyProperty

Allows 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

Name

Type

Description

AllocatedMemoryInfo

MemoryInfo

Data on allocated objects of the Type.

CollectedMemoryInfo

MemoryInfo

Data on collected objects of the Type.

TypeFullyQualifiedName

string

Fully qualified name of the Type.

Type

Type

Type 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: 10 May 2022