共用方式為


IMemoryLeakTrackerService Interface

Definition

Interface to track object in Memory.

public interface class IMemoryLeakTrackerService
public interface IMemoryLeakTrackerService
type IMemoryLeakTrackerService = interface
Public Interface IMemoryLeakTrackerService

Examples

Usage: 1. To track the object you desire to see if it leaks or not, in the constructor of the object call the method TrackObject<T>(T, String, ILivingObjectDiagnostics<T>) Note: This is the only method that should be called in the product code.

[Import]
internal IMemoryLeakTrackerService MemoryLeakTrackerService { get; set; }
MemoryLeakTrackerService.TrackObject<ITextView>(this, this.GetIdentifier(), objectDiagnosticHelper);
where this.GetIdentifier returns a string that identifies the object of the type uniquely.
and objectDiagnosticHelper is a static object of type <xref data-throw-if-not-resolved="true" uid="Microsoft.VisualStudio.Utilities.ILivingObjectDiagnostics`1"></xref>

The implementation of <xref data-throw-if-not-resolved="true" uid="Microsoft.VisualStudio.Utilities.ILivingObjectDiagnostics`1"></xref> cannot contain any instance variables in it.
It should only serve to take in an object of type T to help get its preview or check if its considered to be collected by the component.

2. To check if the object being tracked is leaking or not, for telemetry or test purposes.
var allAliveObjects = MemoryLeakTrackerService.GetLivingObjects<ITextView>();
foreach(var aliveObject in aliveObjects)
{
    if(aliveObject.HasProbablyLeaked())
    {
            // This might be a potential leak
            // Log Telemetry or throw. For Diagnostics use aliveObject.GetPreview();
    }
}

Properties

IsTracking

Flag to start or stop the tracking service

WasCacheResettedBefore

A flag to indicate whether the cache has been resetted before.

Methods

ClearCache()

Clears the cache of all entries. Not to be called from product code. Used by test code or extensions when they have to start off a clear list.

GetLivingObjects()

Gets snapshot of all living objects currently. May require UI thread depending on the object being tracked. Only supposed to be called in Telemetry or Test code.

GetLivingObjects<T>()

Get snapshot of all living object in memory. May require UI thread depending on the object being tracked. Only supposed to be called in Telemetry or Test code.

RefreshWeakRefrenceCache()

Refreshes the cache to get rid of all weak references that are not alive. Usually called before taking a measurement.

TrackObject<T>(T, String, ILivingObjectDiagnostics<T>)

Track object lifecycle in memory. Is free threaded. Called in product code. Does not throw.

Applies to