Class MemoryTracker
- Namespace
- AiDotNet.Diagnostics
- Assembly
- AiDotNet.dll
Tracks memory usage and allocations during ML operations.
public static class MemoryTracker
- Inheritance
-
MemoryTracker
- Inherited Members
Remarks
Features: - GC heap tracking - Working set monitoring - Allocation rate calculation - Memory snapshot comparison
Usage:
// Take a snapshot before an operation
var before = MemoryTracker.Snapshot();
// Run your operation
model.Train(data);
// Take a snapshot after
var after = MemoryTracker.Snapshot();
// Compare
var diff = after.CompareTo(before);
Console.WriteLine($"Memory delta: {diff.TotalMemoryDelta / 1024 / 1024:F2} MB");
Properties
IsEnabled
Gets whether memory tracking is enabled.
public static bool IsEnabled { get; }
Property Value
Methods
Disable()
Disables memory tracking.
public static void Disable()
Enable()
Enables memory tracking.
public static void Enable()
EstimateKVCacheMemory(int, int, int, int, int, int)
Estimates KV-cache memory for a model configuration.
public static long EstimateKVCacheMemory(int numLayers, int numHeads, int headDim, int maxSeqLen, int batchSize = 1, int bytesPerElement = 4)
Parameters
Returns
EstimateTensorMemory(int[], int)
Estimates the memory footprint of a tensor.
public static long EstimateTensorMemory(int[] shape, int elementSize = 4)
Parameters
Returns
- long
Estimated memory in bytes.
GetHistory()
Gets all recorded snapshots.
public static IReadOnlyList<MemorySnapshot> GetHistory()
Returns
GetPressureLevel()
Gets the current memory pressure level.
public static MemoryPressureLevel GetPressureLevel()
Returns
Reset()
Clears all recorded history.
public static void Reset()
Snapshot(string?, bool)
Takes a snapshot of current memory usage.
public static MemorySnapshot Snapshot(string? label = null, bool forceGC = false)
Parameters
labelstringOptional label for this snapshot.
forceGCboolWhether to force garbage collection before measuring.
Returns
- MemorySnapshot
A MemorySnapshot with current memory metrics.
TrackScope(string)
Creates a memory tracking scope that records before/after snapshots.
public static MemoryScope TrackScope(string label)
Parameters
labelstring