{"record":{"id":"001189bf67dcb0bd","repo":"microsoft/garnet","slug":"nameof-logsettings-pagecount-must-be-less-than","errorCode":null,"errorMessage":"{nameof(logSettings.PageCount)} must be less than or equal to the maximum array length ({MemoryUtils.ArrayMaxLength})","messagePattern":"(.+?) must be less than or equal to the maximum array length \\((.+?)\\)","errorType":"exception","errorClass":"TsavoriteException","httpStatus":null,"severity":"error","filePath":"libs/storage/Tsavorite/cs/src/core/Allocator/AllocatorBase.cs","lineNumber":554,"sourceCode":"\n            this.storeFunctions = storeFunctions;\n            _wrapper = wrapperCreator(this);\n\n            // 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))","sourceCodeStart":536,"sourceCodeEnd":572,"githubUrl":"https://github.com/microsoft/garnet/blob/951b0fc6838721f89d102c2bbe1b914e8d39d700/libs/storage/Tsavorite/cs/src/core/Allocator/AllocatorBase.cs#L536-L572","documentation":"Constructor validation: logSettings.PageCount must not exceed MemoryUtils.ArrayMaxLength (0x7FFFFFC7 = 2,147,483,591), because the page ring is backed by a single array indexed by page number. A PageCount above this cannot be allocated as a .NET array.","triggerScenarios":"Setting `logSettings.PageCount = int.MaxValue` or computing it from a huge MemorySize expression and assigning a value above 0x7FFFFFC7.","commonSituations":"Trying to pre-size the buffer for an extreme workload by pushing PageCount to int.MaxValue; unit confusion (bytes vs pages) yielding billions of pages.","solutions":["Reduce PageCount to <= 0x7FFFFFC7; in practice keep it modest (hundreds to thousands of pages).","If you derived PageCount from MemorySize, prefer leaving PageCount = 0 and letting the allocator compute it as MemorySize / PageSize.","Re-check the arithmetic that produced the large number (likely a bytes/pages unit error)."],"exampleFix":"// before\nlogSettings.PageCount = int.MaxValue;\n// after\nlogSettings.PageCount = 0;                 // let allocator derive from MemorySize\n// or\nlogSettings.PageCount = 4096;","handlingStrategy":"validation","validationCode":"const int ArrayMaxLength = 0x7FFFFFC7; // MemoryUtils.ArrayMaxLength\nif (logSettings.PageCount > ArrayMaxLength)\n    throw new ArgumentOutOfRangeException(nameof(logSettings.PageCount),\n        $\"PageCount {logSettings.PageCount} exceeds ArrayMaxLength {ArrayMaxLength}\");","typeGuard":null,"tryCatchPattern":"try { new TsavoriteKV<K,V>(logSettings, ...); }\ncatch (TsavoriteException ex) when (ex.Message.Contains(\"PageCount\") && ex.Message.Contains(\"maximum array length\"))\n{ /* reduce PageCount or leave it 0 to derive from MemorySize */ }","preventionTips":["Prefer leaving PageCount=0 and sizing via MemorySize.","Clamp config-sourced PageCount to a sane ceiling (e.g. 1_000_000) before assignment.","Verify bytes-vs-pages units when computing PageCount."],"tags":["storage","configuration","log-settings","limits"],"backgroundTag":null,"analyzedSha":"951b0fc6838721f89d102c2bbe1b914e8d39d700","analyzedAt":"2026-08-13T19:01:32.939Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}