microsoft/garnet · error · TsavoriteException

{nameof(logSettings.MemorySize)} must be between {1L << LogS

Error message

{nameof(logSettings.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}, or may be 0 for ReadOnly TsavoriteLog

What it means

Constructor validation: when logSettings.MemorySize is non-zero it must lie in [1<<kMinMemorySizeBits, 1<<kMaxMemorySizeBits] = [1<<13, 1<<62] = [8KiB, 4EiB]. 0 is explicitly allowed for ReadOnly TsavoriteLog. Values outside the range are rejected because the memory tracker and page-count derivation assume this bound. Default is 1L<<34 (16GiB).

Source

Thrown at libs/storage/Tsavorite/cs/src/core/Allocator/AllocatorBase.cs:558

            // Pre-bind the instance-method delegate once so the pending-IO hot path does
            // not allocate a fresh DeviceIOCompletionCallback per AsyncGetFromDisk call.
            asyncGetFromDiskCallbackDelegate = AsyncGetFromDiskCallback;

            this.transientObjectIdMap = transientObjectIdMap;

            // Validation
            if (logSettings.PageCount == 0 && logSettings.MemorySize == 0)
                throw new TsavoriteException($"{nameof(logSettings.PageCount)} or {nameof(logSettings.MemorySize)} must be specified");
            if (logSettings.PageSizeBits < LogSettings.kMinPageSizeBits || logSettings.PageSizeBits > LogSettings.kMaxPageSizeBits)
                throw new TsavoriteException($"{nameof(logSettings.PageSizeBits)} must be between {LogSettings.kMinPageSizeBits} and {LogSettings.kMaxPageSizeBits}");
            if (logSettings.PageSizeBits < PageHeader.SizeBits)
                throw new TsavoriteException($"{nameof(logSettings.PageSizeBits)} must be >= PageHeader.SizeBits {PageHeader.SizeBits}");
            if (logSettings.PageCount > MemoryUtils.ArrayMaxLength)
                throw new TsavoriteException($"{nameof(logSettings.PageCount)} must be less than or equal to the maximum array length ({MemoryUtils.ArrayMaxLength})");
            if (logSettings.SegmentSizeBits < LogSettings.kMinMainLogSegmentSizeBits || logSettings.SegmentSizeBits > LogSettings.kMaxSegmentSizeBits)
                throw new TsavoriteException($"{nameof(logSettings.SegmentSizeBits)} must be between {LogSettings.kMinMainLogSegmentSizeBits} and {LogSettings.kMaxSegmentSizeBits}");
            if (logSettings.MemorySize != 0 && (logSettings.MemorySize < 1L << LogSettings.kMinMemorySizeBits || logSettings.MemorySize > 1L << LogSettings.kMaxMemorySizeBits))
                throw new TsavoriteException($"{nameof(logSettings.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}, or may be 0 for ReadOnly TsavoriteLog");
            if ((logSettings.MemorySize != 0) && (logSettings.MemorySize < (1L << logSettings.PageSizeBits) * LogSettings.kMinPageCount))
                throw new TsavoriteException($"{nameof(logSettings.MemorySize)} must be at least {LogSettings.kMinPageCount}x the page size ({1L << logSettings.PageSizeBits})");
            if (logSettings.MutableFraction < 0.0 || logSettings.MutableFraction > 1.0)
                throw new TsavoriteException($"{nameof(logSettings.MutableFraction)} must be >= 0.0 and <= 1.0");
            if (logSettings.ReadCacheSettings is not null)
            {
                var rcs = logSettings.ReadCacheSettings;
                if (rcs.PageCount == 0 && rcs.MemorySize == 0)
                    throw new TsavoriteException($"{nameof(rcs.PageCount)} or {nameof(rcs.MemorySize)} must be specified");
                if (rcs.PageSizeBits < LogSettings.kMinPageSizeBits || rcs.PageSizeBits > LogSettings.kMaxPageSizeBits)
                    throw new TsavoriteException($"{nameof(rcs.PageSizeBits)} must be between {LogSettings.kMinPageSizeBits} and {LogSettings.kMaxPageSizeBits}");
                if (rcs.PageCount > MemoryUtils.ArrayMaxLength)
                    throw new TsavoriteException($"{nameof(rcs.PageCount)} must be less than or equal to the maximum array length ({MemoryUtils.ArrayMaxLength})");
                if (rcs.MemorySize != 0 && (rcs.MemorySize < 1L << LogSettings.kMinMemorySizeBits || rcs.MemorySize > 1L << LogSettings.kMaxMemorySizeBits))
                    throw new TsavoriteException($"{nameof(rcs.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}");
                if ((rcs.MemorySize != 0) && (rcs.MemorySize < (1L << rcs.PageSizeBits) * 2))
                    throw new TsavoriteException($"{nameof(logSettings.MemorySize)} must be at least twice the page size ({1L << rcs.PageSizeBits})");
                if (rcs.SecondChanceFraction < 0.0 || rcs.SecondChanceFraction > 1.0)

View on GitHub (pinned to 951b0fc683)

Solutions

  1. Set MemorySize to 0 only for ReadOnly logs; otherwise use a value >= 8KiB (default 16GiB is typical).
  2. Keep MemorySize below 1<<62 (never an issue in practice).
  3. Double check that the number is in bytes (16 * 1024L * 1024 * 1024 for 16GiB).

Example fix

// before
logSettings.MemorySize = 4096;          // below 8KiB floor
// after
logSettings.MemorySize = 1L << 24;      // 16MiB, comfortably above floor
Defensive patterns

Strategy: validation

Validate before calling

const long MinMem = 1L << 13, MaxMem = 1L << 62; // [8KiB, 4EiB]
if (logSettings.MemorySize != 0 && (logSettings.MemorySize < MinMem || logSettings.MemorySize > MaxMem))
    throw new ArgumentOutOfRangeException(nameof(logSettings.MemorySize),
        $"MemorySize must be 0 (ReadOnly) or in [{MinMem},{MaxMem}]");

Try / catch

try { new TsavoriteKV<K,V>(logSettings, ...); }
catch (TsavoriteException ex) when (ex.Message.Contains("MemorySize") && ex.Message.Contains("between"))
{ /* set MemorySize to 0 (ReadOnly) or >= 8KiB and recreate */ }

Prevention

When it happens

Trigger: Setting MemorySize to a small positive value below 8KiB (e.g. 4096), or to an absurdly large value above 1<<62, while constructing a read/write store.

Common situations: Unit-test config that set MemorySize = 4096 to keep RAM tiny; computing MemorySize from MB/GB multipliers and underflowing to a sub-8KiB number; mis-typing bytes vs KiB.

Related errors


AI-assisted analysis of microsoft/garnet@951b0fc683 (2026-08-13). Data as JSON: /api/errors/6838845cae37f616. Report an issue: GitHub.