Statistics

ResolveStatistics returns diagnostic data about the database files. Pass a Stats flags value to select which metrics to populate; unrequested properties are null on the returned MemDbStatistics. Each requested flag triggers its own pass over the pointer list under a shared lock, so cost scales with dataset size and flag count. Request only what you need.

CategoryFlagTypeDescription
CountFreshCountint?Number of fresh records
StaleCountint?Records superseded by updates
DeletedCountint?Records marked deleted but not yet purged
Total sizeFreshSizelong?Total byte footprint of fresh records
StaleSizelong?Total byte footprint of stale records
DeletedSizelong?Total byte footprint of deleted records
Max sizeMaxFreshSizeint?Largest fresh record in bytes
MaxStaleSizeint?Largest stale record in bytes
MaxDeletedSizeint?Largest deleted record in bytes
Min sizeMinFreshSizeint?Smallest fresh record in bytes
MinStaleSizeint?Smallest stale record in bytes
MinDeletedSizeint?Smallest deleted record in bytes
Avg sizeAvgFreshSizedouble?Average fresh record size in bytes
AvgStaleSizedouble?Average stale record size in bytes
AvgDeletedSizedouble?Average deleted record size in bytes
IdentityLastIdlong?Last auto-increment id assigned
Stats resolve = Stats.FreshCount | Stats.StaleCount | Stats.DeletedCount
              | Stats.FreshSize  | Stats.StaleSize  | Stats.DeletedSize
              | Stats.LastId;

using (var db = MemDb.Open<Person>("people"))
{
    MemDbStatistics stats = db.ResolveStatistics(resolve);
}

ResolveStatistics is only available on persisted databases — calling it on a volatile (no-path) instance throws.

Common patterns

Detecting write amplification. The ratio of StaleSize + DeletedSize to FreshSize is a useful proxy for how much disk space is being held by superseded record versions. When this ratio exceeds a chosen threshold, schedule a defrag.

var s = db.ResolveStatistics(Stats.FreshSize | Stats.StaleSize | Stats.DeletedSize);

// Requested flags guarantee non-null — .Value is safe here.
double waste = (double)(s.StaleSize!.Value + s.DeletedSize!.Value) / s.FreshSize!.Value;

Deciding when to purge the cache. In long-running instances, monitor StaleCount + DeletedCount and call PurgeCache when accumulation exceeds a threshold proportional to your fresh record count.

Capacity planning. The .map file overhead is 12 + (39 × total record count) bytes regardless of record size or content. Combined with FreshSize + StaleSize + DeletedSize for the .db file, this gives an exact on-disk footprint.