Work with Object Sets
tip
The objects that are no longer needed by a program may be collected by garbage collector during a test run. To intentionally prevent an object from collection, use the
GC.KeepAlive
method.
Object set is a number of objects selected by specific condition. To ease the understanding, think of an object set as of the result of some query, like "Select all objects of the string type promoted to Gen 2", or "Select all objects retained in memory by the instance A", and so on. An ObjectSet
instance is returned as a result of the ObjectSet.GetObjects
and ObjectSet.GetExclusivelyRetainedObjects
methods.
To slice data by a number of conditions, you can build chains of GetObjects
calls. The ObjectSet
type has two properties that you can use in test assertions: ObjectsCount
and SizeInBytes
.
For example, the following code asserts that there are no string
and Foo
objects in the Large Object Heap:
dotMemory.Check(memory =>
{
Assert.That(memory.GetObjects(where => where.Type.Is(typeof(string), typeof(Foo)))
.GetObjects(where => where.Generation.Is(Generation.LOH)).ObjectsCount, Is.EqualTo(0));
});
dotMemory Unit also provides alternative syntax for object set queries. You can use the ==
, &
, and |
logic operators to combine a number of queries. For example:
Assert.That(memory.GetObjects(where => where.Type == (typeof(string)) &
where.Generation == Generation.LOH).ObjectsCount, Is.EqualTo(0));
Represents a set of objects in memory.
Name | Description |
---|---|
| Gets a subset of objects by a specific condition. The condition is defined by a Returns an instance of the |
| Gets a subset of objects by a specific condition. The condition is defined by a reusable query - an instance of the ObjectSetQuery type. For more information, refer to QueryBuilder. Returns an instance of the |
| Gets a set of objects that are exclusively retained (dominated) by the current object set. Returns an instance of the |
| 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. |
Name | Type | Description |
---|---|---|
|
| Total number of objects in the set. |
|
| Total size of objects in the set in bytes. |
Selects objects in a set by a specific condition: type, interface, and others.
Name | Description |
---|---|
| Creates the "Select all objects that cause the event handlers leak" query. Learn more. Returns |
| Creates the query "Select all objects that are not exclusively retained in memory (in other words, objects that are retained by more than one object)". Returns |
Name | Type | Description |
---|---|---|
|
| Allows selecting objects by type. |
|
| Allows selecting objects by interface. |
|
| Allows selecting objects by namespace. |
|
| Allows selecting objects by assembly. |
|
| Allows selecting objects by generation. |
TypeProperty
is used to create "select objects by type" queries.
TypeProperty Methods
Name | Description |
---|---|
| Creates the "Select all objects of particular types" query. The list of types is passed in a Returns |
| Creates the "Select all objects of a particular type" query. Open generic types are also supported. Returns |
| Creates the "Select all objects that do not belong to particular types" query. The list of types is passed in a Returns |
| Creates the "Select all objects that do not belong to a particular type" query. Open generic types are also supported. Returns |
| Creates the "Select all objects of particular types" query. The list of types is passed in a Returns |
| Creates the "Select all objects that do not belong to particular types" query. The list of types is passed in a Returns |
InterfaceProperty
is used to create "select objects by interface" queries.
InterfaceProperty Methods
Name | Description |
---|---|
| Creates the "Select all objects that implement particular interfaces" query. The list of interfaces is passed in a Returns |
| Creates the "Select all objects that implement a particular interface" query. Open generic interfaces are also supported. Returns |
| Creates the "Select all objects that do not implement particular interfaces" query. The list of interfaces is passed in a Returns |
| Creates the "Select all objects that do not implement a particular interface" query. Open generic interfaces are also supported. Returns |
| Creates the "Select all objects that implement particular interfaces" query. The list of interfaces is passed in a Returns |
| Creates the "Select all objects that implement particular interfaces" query. The list of interfaces is passed in a Returns |
NamespaceProperty
is used to create "select objects by namespace" queries.
Name | Description |
---|---|
| Creates the "Select all objects that belong to particular namespaces" query. The list of namespaces is passed in a Returns |
| Creates the "Select all objects that do not belong to particular namespaces" query. The list of namespaces is passed in a Returns |
AssemblyProperty
is used to create "select objects by assembly" queries.
Name | Description |
---|---|
| Creates the "Select all objects that belong to particular assemblies" query. The list of assemblies is passed in a Returns |
| Creates the "Select all objects that do not belong to particular assemblies" query. The list of assemblies is passed in a Returns |
| Creates the "Select all objects that belong to particular assemblies" query. The list of assemblies is passed in a Returns |
| Creates the "Select all objects that do not belong to particular assemblies" query. The list of assemblies is passed in a Returns |
GenerationProperty
is used to create "select objects by generation" queries.
Name | Description |
---|---|
| Creates the "Select all objects from particular managed heap segments" query. The list of heap segments is passed in a Returns |
| Creates the "Select all objects that do not belong to particular managed heap segments" query. The list of heap segments is passed in a Returns |
Represents certain managed heap segments:
Value | Description |
---|---|
| Generation 1 heap segment. |
| Generation 2 heap segment. |
| Large object heap. |
TypeMemoryInfo
carries info about a group of objects of the same type: a number of objects, and their size. Instances of the TypeMemoryInfo
class are a result of objects grouping via the GroupByType()
method.
Name | Type | Description |
---|---|---|
|
| Total number of objects of the |
|
| Total size of objects of the |
|
| Fully qualified name of the |
|
| Type of objects represented by this instance of the Can be |