Table of Contents

Class MemoryStatistics

Namespace
DotCompute.Memory
Assembly
DotCompute.Memory.dll

CONSOLIDATED memory usage and performance statistics. This replaces all duplicate MemoryStatistics implementations across the codebase.

Provides comprehensive tracking for:

  • Allocation and deallocation counts
  • Memory usage (current, peak, total)
  • Pool efficiency metrics
  • Performance timing data
  • Error and failure statistics
public sealed class MemoryStatistics
Inheritance
MemoryStatistics
Inherited Members

Properties

ActiveAllocations

Gets the number of active allocations (allocations - deallocations).

public long ActiveAllocations { get; }

Property Value

long

AverageAllocationSize

Gets the average allocation size in bytes.

public double AverageAllocationSize { get; }

Property Value

double

AverageAllocationTime

Gets the average time taken for allocation operations in milliseconds.

public double AverageAllocationTime { get; }

Property Value

double

AverageCopyTime

Gets the average time taken for copy operations in milliseconds.

public double AverageCopyTime { get; }

Property Value

double

AverageDeallocationTime

Gets the average time taken for deallocation operations in milliseconds.

public double AverageDeallocationTime { get; }

Property Value

double

CopyOperations

Gets the total number of copy operations performed.

public long CopyOperations { get; }

Property Value

long

CurrentlyAllocatedBytes

Gets the number of bytes currently allocated and not yet freed.

public long CurrentlyAllocatedBytes { get; }

Property Value

long

FailedAllocations

Gets the number of failed allocation attempts.

public long FailedAllocations { get; }

Property Value

long

MemoryEfficiency

Gets the memory efficiency ratio (bytes freed / bytes allocated). A value of 1.0 indicates all allocated memory has been freed.

public double MemoryEfficiency { get; }

Property Value

double

PeakAllocatedBytes

Gets the peak number of bytes allocated at any point in time.

public long PeakAllocatedBytes { get; }

Property Value

long

PoolHitRate

Gets the memory pool hit rate (percentage of allocations satisfied by the pool).

public double PoolHitRate { get; }

Property Value

double

PoolHits

Gets the number of allocations satisfied by the memory pool.

public long PoolHits { get; }

Property Value

long

PoolMisses

Gets the number of allocations that could not be satisfied by the memory pool.

public long PoolMisses { get; }

Property Value

long

TotalAllocations

Gets the total number of allocation operations performed.

public long TotalAllocations { get; }

Property Value

long

TotalBytesAllocated

Gets the total number of bytes that have been allocated (cumulative).

public long TotalBytesAllocated { get; }

Property Value

long

TotalBytesFreed

Gets the total number of bytes that have been freed (cumulative).

public long TotalBytesFreed { get; }

Property Value

long

TotalDeallocations

Gets the total number of deallocation operations performed.

public long TotalDeallocations { get; }

Property Value

long

Methods

CreateSnapshot()

Creates a snapshot of the current statistics. This is useful for reporting without affecting the ongoing statistics collection.

public MemoryStatistics CreateSnapshot()

Returns

MemoryStatistics

A new MemoryStatistics instance containing the current values.

RecordAllocation(long, double, bool)

Records a successful allocation operation.

public void RecordAllocation(long bytes, double timeMs, bool fromPool)

Parameters

bytes long

The number of bytes allocated.

timeMs double

The time taken for the allocation in milliseconds.

fromPool bool

Whether the allocation was satisfied by the memory pool.

RecordBufferCreation(long)

Records buffer creation (for tracking buffer lifecycle).

public static void RecordBufferCreation(long bytes)

Parameters

bytes long

The size of the buffer created.

RecordCopyOperation(long, double, bool)

Records a memory copy operation.

public void RecordCopyOperation(long bytes, double timeMs, bool isHostToDevice)

Parameters

bytes long

The number of bytes copied.

timeMs double

The time taken for the copy operation in milliseconds.

isHostToDevice bool

Whether this was a host-to-device transfer.

RecordDeallocation(long, double)

Records a successful deallocation operation.

public void RecordDeallocation(long bytes, double timeMs = 0)

Parameters

bytes long

The number of bytes deallocated.

timeMs double

The time taken for the deallocation in milliseconds (optional).

RecordFailedAllocation(long)

Records a failed allocation attempt.

public void RecordFailedAllocation(long requestedBytes)

Parameters

requestedBytes long

The number of bytes that were requested but could not be allocated.

Reset()

Resets all statistics to zero. Use with caution as this will clear all historical data.

public void Reset()

ToDetailedString()

Gets detailed statistics for diagnostic purposes.

public string ToDetailedString()

Returns

string

A detailed string representation of all statistics.

ToString()

Gets a summary of the current memory statistics.

public override string ToString()

Returns

string

A formatted string containing key statistics.