Fix NullReferenceException in SegmentedList.Add during large heap snapshots#2391
Draft
Fix NullReferenceException in SegmentedList.Add during large heap snapshots#2391
Conversation
…pshots SegmentedMemoryStreamWriter.Clear() was discarding the pre-allocated bytes buffer and replacing it with a new empty SegmentedList. For large heap dumps (128M+ objects), this caused: - The ~1GB pre-allocation from the constructor to be wasted immediately - A new list with items=null and a different segmentSize (131072 vs 65536) - NullReferenceException in SegmentedList.Add when the fresh empty list was used under memory pressure Fix: reset bytes.Count = 0 to reuse the existing pre-allocated buffer. Also adds SegmentedMemoryStreamWriterTests covering Clear() behavior, write/read round-trips, and cross-segment-boundary writes. Co-authored-by: brianrob <6210322+brianrob@users.noreply.github.com> Agent-Logs-Url: https://github.com/microsoft/perfview/sessions/de8bc1dc-8c49-45aa-80a2-0cd73d4a3536
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
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.