{"record":{"id":"6838845cae37f616","repo":"microsoft/garnet","slug":"nameof-logsettings-memorysize-must-be-between","errorCode":null,"errorMessage":"{nameof(logSettings.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}, or may be 0 for ReadOnly TsavoriteLog","messagePattern":"(.+?) must be between (.+?) and (.+?), or may be 0 for ReadOnly TsavoriteLog","errorType":"exception","errorClass":"TsavoriteException","httpStatus":null,"severity":"error","filePath":"libs/storage/Tsavorite/cs/src/core/Allocator/AllocatorBase.cs","lineNumber":558,"sourceCode":"            // Pre-bind the instance-method delegate once so the pending-IO hot path does\n            // not allocate a fresh DeviceIOCompletionCallback per AsyncGetFromDisk call.\n            asyncGetFromDiskCallbackDelegate = AsyncGetFromDiskCallback;\n\n            this.transientObjectIdMap = transientObjectIdMap;\n\n            // Validation\n            if (logSettings.PageCount == 0 && logSettings.MemorySize == 0)\n                throw new TsavoriteException($\"{nameof(logSettings.PageCount)} or {nameof(logSettings.MemorySize)} must be specified\");\n            if (logSettings.PageSizeBits < LogSettings.kMinPageSizeBits || logSettings.PageSizeBits > LogSettings.kMaxPageSizeBits)\n                throw new TsavoriteException($\"{nameof(logSettings.PageSizeBits)} must be between {LogSettings.kMinPageSizeBits} and {LogSettings.kMaxPageSizeBits}\");\n            if (logSettings.PageSizeBits < PageHeader.SizeBits)\n                throw new TsavoriteException($\"{nameof(logSettings.PageSizeBits)} must be >= PageHeader.SizeBits {PageHeader.SizeBits}\");\n            if (logSettings.PageCount > MemoryUtils.ArrayMaxLength)\n                throw new TsavoriteException($\"{nameof(logSettings.PageCount)} must be less than or equal to the maximum array length ({MemoryUtils.ArrayMaxLength})\");\n            if (logSettings.SegmentSizeBits < LogSettings.kMinMainLogSegmentSizeBits || logSettings.SegmentSizeBits > LogSettings.kMaxSegmentSizeBits)\n                throw new TsavoriteException($\"{nameof(logSettings.SegmentSizeBits)} must be between {LogSettings.kMinMainLogSegmentSizeBits} and {LogSettings.kMaxSegmentSizeBits}\");\n            if (logSettings.MemorySize != 0 && (logSettings.MemorySize < 1L << LogSettings.kMinMemorySizeBits || logSettings.MemorySize > 1L << LogSettings.kMaxMemorySizeBits))\n                throw new TsavoriteException($\"{nameof(logSettings.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}, or may be 0 for ReadOnly TsavoriteLog\");\n            if ((logSettings.MemorySize != 0) && (logSettings.MemorySize < (1L << logSettings.PageSizeBits) * LogSettings.kMinPageCount))\n                throw new TsavoriteException($\"{nameof(logSettings.MemorySize)} must be at least {LogSettings.kMinPageCount}x the page size ({1L << logSettings.PageSizeBits})\");\n            if (logSettings.MutableFraction < 0.0 || logSettings.MutableFraction > 1.0)\n                throw new TsavoriteException($\"{nameof(logSettings.MutableFraction)} must be >= 0.0 and <= 1.0\");\n            if (logSettings.ReadCacheSettings is not null)\n            {\n                var rcs = logSettings.ReadCacheSettings;\n                if (rcs.PageCount == 0 && rcs.MemorySize == 0)\n                    throw new TsavoriteException($\"{nameof(rcs.PageCount)} or {nameof(rcs.MemorySize)} must be specified\");\n                if (rcs.PageSizeBits < LogSettings.kMinPageSizeBits || rcs.PageSizeBits > LogSettings.kMaxPageSizeBits)\n                    throw new TsavoriteException($\"{nameof(rcs.PageSizeBits)} must be between {LogSettings.kMinPageSizeBits} and {LogSettings.kMaxPageSizeBits}\");\n                if (rcs.PageCount > MemoryUtils.ArrayMaxLength)\n                    throw new TsavoriteException($\"{nameof(rcs.PageCount)} must be less than or equal to the maximum array length ({MemoryUtils.ArrayMaxLength})\");\n                if (rcs.MemorySize != 0 && (rcs.MemorySize < 1L << LogSettings.kMinMemorySizeBits || rcs.MemorySize > 1L << LogSettings.kMaxMemorySizeBits))\n                    throw new TsavoriteException($\"{nameof(rcs.MemorySize)} must be between {1L << LogSettings.kMinMemorySizeBits} and {1L << LogSettings.kMaxMemorySizeBits}\");\n                if ((rcs.MemorySize != 0) && (rcs.MemorySize < (1L << rcs.PageSizeBits) * 2))\n                    throw new TsavoriteException($\"{nameof(logSettings.MemorySize)} must be at least twice the page size ({1L << rcs.PageSizeBits})\");\n                if (rcs.SecondChanceFraction < 0.0 || rcs.SecondChanceFraction > 1.0)","sourceCodeStart":540,"sourceCodeEnd":576,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/storage/Tsavorite/cs/src/core/Allocator/AllocatorBase.cs#L540-L576","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Set MemorySize to 0 only for ReadOnly logs; otherwise use a value >= 8KiB (default 16GiB is typical).","Keep MemorySize below 1<<62 (never an issue in practice).","Double check that the number is in bytes (16 * 1024L * 1024 * 1024 for 16GiB)."],"exampleFix":"// before\nlogSettings.MemorySize = 4096;          // below 8KiB floor\n// after\nlogSettings.MemorySize = 1L << 24;      // 16MiB, comfortably above floor","handlingStrategy":"validation","validationCode":"const long MinMem = 1L << 13, MaxMem = 1L << 62; // [8KiB, 4EiB]\nif (logSettings.MemorySize != 0 && (logSettings.MemorySize < MinMem || logSettings.MemorySize > MaxMem))\n    throw new ArgumentOutOfRangeException(nameof(logSettings.MemorySize),\n        $\"MemorySize must be 0 (ReadOnly) or in [{MinMem},{MaxMem}]\");","typeGuard":null,"tryCatchPattern":"try { new TsavoriteKV<K,V>(logSettings, ...); }\ncatch (TsavoriteException ex) when (ex.Message.Contains(\"MemorySize\") && ex.Message.Contains(\"between\"))\n{ /* set MemorySize to 0 (ReadOnly) or >= 8KiB and recreate */ }","preventionTips":["Use 0 only for ReadOnly logs; otherwise keep MemorySize >= 8KiB (default 16GiB).","Express memory budgets in bytes via KiB/MiB/GiB shifts to avoid underflow.","Validate config-loaded values before assigning."],"tags":["storage","configuration","log-settings","memory"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}