Fix NullReferenceException in SegmentedList.Add during large heap snapshots#2391
Closed
Fix NullReferenceException in SegmentedList.Add during large heap snapshots#2391
Conversation
Copilot
AI
changed the title
[WIP] Fix System.NullReferenceException while taking heap snapshot
Fix NullReferenceException in SegmentedList.Add during large heap snapshots
Mar 20, 2026
c5fc80f to
adabae6
Compare
…pshots SegmentedMemoryStreamWriter.Clear() was replacing the backing bytes list with a brand-new empty SegmentedList, discarding the pre-allocated buffer (potentially ~1GB for 128M-node graphs). The replacement started with items=null and capacity=0, requiring re-growth from scratch under extreme memory pressure, which caused NullReferenceException in SegmentedList.Add. The fix resets bytes.Count to 0 instead, preserving the pre-allocated segments. This is safe because SegmentedList.Add() checks count < capacity and writes directly into existing segments. This pattern is already used in Graph.ClearWorker() (m_nodes.Count = 0, m_types.Count = 0). Also fixes a segment-size inconsistency: the constructor used 65_536 but the old Clear() was creating a new list with 131_072. Fixes #951 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
adabae6 to
7b47692
Compare
Member
|
This does not look to be complete. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Taking a heap snapshot on large heaps (100M+ objects) throws
System.NullReferenceExceptioninsideSegmentedList<T>.AddviaGraph.SetNodeTypeAndSize→Node.WriteCompressedInt.Root Cause
SegmentedMemoryStreamWriter.Clear()was replacing the backingbyteslist with a brand-new emptySegmentedList<byte>:Graph.ClearWorker()creates the writer with a large initial capacity (m_expectedNodeCount * 8, up to ~1 GB for 128M-node graphs) and then immediately callsm_writer.Clear(), discarding that entire pre-allocation. The replacement list starts withitems = null. WhenWrite(byte)is JIT-inlined intoWriteCompressedInt, callingbytes.Add(value)on this fresh null-items list under memory pressure results in aNullReferenceExceptioninsideSegmentedList.Add. There's also a segment-size inconsistency: the constructor usessegmentSize = 65_536;Clear()was creating a new list withsegmentSize = 131_072.Fix
Reset the count of the existing
byteslist rather than replacing it:This preserves the pre-allocated buffer (eliminating the wasted ~1 GB allocation and the
items = nullstate), keeps the segment size consistent, and removes the NullReferenceException risk.Changes
src/FastSerialization/SegmentedMemoryStreamWriter.cs— one-line fix toClear()src/FastSerialization.Tests/SegmentedMemoryStreamWriterTests.cs— new tests coveringClear()semantics, write/read round-trips, multiple clear cycles, and cross-segment-boundary writesOriginal prompt
⌨️ Start Copilot coding agent tasks without leaving your editor — available in VS Code, Visual Studio, JetBrains IDEs and Eclipse.