Table of Contents

Class PixelMemoryUsage

Namespace
Egami
Assembly
Egami.dll

What a PooledPixelMemorySource has handed out and not yet had back, and opt-in tracking of blocks that were never disposed. Get it from Usage; most callers want PooledPixelMemorySource.Shared.Usage.

public sealed class PixelMemoryUsage
Inheritance
PixelMemoryUsage
Inherited Members

Examples

PixelMemoryUsage usage = PooledPixelMemorySource.Shared.Usage;
usage.CaptureStackTraces = true;
usage.LeakDetected += (_, leak) => Console.WriteLine($"{leak.Bytes} bytes never disposed, allocated at {leak.StackTrace}");
usage.TrackLeaks = true;
RunTheCodeUnderSuspicion();
Console.WriteLine($"{usage.LiveBlocks} blocks, {usage.LiveBytes} bytes still out");

Remarks

The counts are always kept, at the cost of one atomic addition per allocation and per return. Leak tracking adds a small object with a finalizer to every block allocated while it is on, so turn it on only while looking for a leak.

Properties

CaptureStackTraces

Gets or sets whether tracked blocks record the stack trace of their allocation for LeakDetected. It makes every allocation much slower. Defaults to false.

public bool CaptureStackTraces { get; set; }

Property Value

bool

LiveBlocks

Gets the number of blocks handed out and not yet disposed.

public long LiveBlocks { get; }

Property Value

long

LiveBytes

Gets the bytes in blocks handed out and not yet disposed.

public long LiveBytes { get; }

Property Value

long

TrackLeaks

Gets or sets whether blocks allocated from now on are tracked, so that one collected without being disposed raises LeakDetected and leaves the counts. Defaults to false.

public bool TrackLeaks { get; set; }

Property Value

bool

Events

LeakDetected

Occurs when a block allocated while TrackLeaks was on is collected by the garbage collector without having been disposed. Handlers run on the finalizer thread, so they must be quick, must not throw, and must not touch objects that may already be finalized. Leaks are reported here, not through Diagnostics: they belong to the memory source and are found after any call has ended.

public event EventHandler<PixelMemoryLeakEventArgs>? LeakDetected

Event Type

EventHandler<PixelMemoryLeakEventArgs>