MemoryTracker

Helpers & Utilities · AiDotNet.Diagnostics

Tracks memory usage and allocations during ML operations.

How It Works

Features:

  • GC heap tracking
  • Working set monitoring
  • Allocation rate calculation
  • Memory snapshot comparison

Usage:

Properties

Property Summary
bool IsEnabled { get; } Gets whether memory tracking is enabled.
int MaxHistorySize { get; set; } Gets or sets the maximum history size (default: 10000).

Methods

Method Summary
IReadOnlyList<MemorySnapshot> GetHistory() Gets all recorded snapshots.
MemoryPressureLevel GetPressureLevel() Gets the current memory pressure level.
MemoryScope TrackScope(string label) Creates a memory tracking scope that records before/after snapshots.
MemorySnapshot Snapshot(string label, bool forceGC) Takes a snapshot of current memory usage.
long EstimateKVCacheMemory(int numLayers, int numHeads, int headDim, int maxSeqLen, int batchSize, int bytesPerElement) Estimates KV-cache memory for a model configuration.
long EstimateTensorMemory(int[] shape, int elementSize) Estimates the memory footprint of a tensor.
void Disable() Disables memory tracking.
void Enable() Enables memory tracking.
void Reset() Clears all recorded history.