Method FastSnapshot
FastSnapshot(Boolean)
Performs a fast snapshot of the garbage collected heap. The returned MemSnapshot can be used in subsequent calls to the MemAssertion and MemProfiler.GetMemoryUsage methods.
Declaration
public static MemSnapshot FastSnapshot(bool storeAsBaseSnapshot)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | storeAsBaseSnapshot | Indicates whether the snapshot should be stored as a base snapshot for
subsequent allocations. By default this parameter is |
Returns
Type | Description |
---|---|
MemSnapshot | If profiling is active, a MemSnapshot instance representing the current state of the garbage collected heap. Otherwise Empty will be returned. |
Remarks
If the current process is attached to the profiler, a garbage collection will be performed and a MemSnapshot instance representing the state of the GC heap after the collection will be returned. The returned MemSnapshot can be used as a reference snapshot in subsequent calls to the MemAssertion methods.
If the current process is not currently being profiled, this method does nothing and returns Empty
note
A snapshot consumes a significant amount of memory. If storeAsBaseSnapshot
is true
the snapshot cannot be garbage collected until a new snapshot is collected.
It is recommended that the base snapshot is explicitly specified when calling the assertion methods or retrieving memory usage, instead of storing it as a base snapshot.
Examples
void TestMemory()
{
// Collect a heap snapshot to use as reference.
MemSnapshot snapshot = MemProfiler.FastSnapshot();
// Call some method that should not create new live instances
TestMethod();
// Make sure that no new live instances were created.
MemAssertion.NoNewInstances( snapshot );
}
void TestUsingMemoryUsage()
{
// Collect a heap snapshot to use as reference.
MemSnapshot snapshot = MemProfiler.FastSnapshot();
// Call some method that should not create new live instances
TestMethod();
// Make sure that no new live instances were created.
MemoryUsage memUsage = MemProfiler.GetMemoryUsage( snapshot);
InstancesInfo instancesInfo = memUsage.GetLiveInstancesInfo();
Assert.AreEqual( 0, instancesInfo.NewInstancesCount );
}
FastSnapshot()
Performs a fast snapshot of the garbage collected heap. The returned MemSnapshot can be used in subsequent calls to the MemAssertion and MemProfiler.GetMemoryUsage methods.
Declaration
public static MemSnapshot FastSnapshot()
Returns
Type | Description |
---|---|
MemSnapshot | If profiling is active, a MemSnapshot instance representing the current state of the garbage collected heap. Otherwise Empty will be returned. |
Remarks
If the current process is attached to the profiler, a garbage collection will be performed and a MemSnapshot instance representing the state of the GC heap after the collection will be returned. The returned MemSnapshot can be used as a reference snapshot in subsequent calls to the MemAssertion methods.
If the current process is not currently being profiled, this method does nothing and returns Empty
note
A snapshot consumes a significant amount of memory. If storeAsBaseSnapshot
is true
the snapshot cannot be garbage collected until a new snapshot is collected.
It is recommended that the base snapshot is explicitly specified when calling the assertion methods or retrieving memory usage, instead of storing it as a base snapshot.