Document MemoryStream maximum capacity breaking change in .NET 11#50992
Merged
Document MemoryStream maximum capacity breaking change in .NET 11#50992
Conversation
…NET 11 Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Update MemoryStream max capacity and exception behavior
Document MemoryStream maximum capacity breaking change in .NET 11
Jan 8, 2026
gewarren
approved these changes
Jan 9, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR documents a breaking change in .NET 11 Preview 1 where MemoryStream now enforces a maximum capacity of 0x7FFFFFC7 bytes and throws ArgumentOutOfRangeException instead of OutOfMemoryException for invalid capacity/length values.
Key changes:
- Created comprehensive breaking change documentation explaining the exception behavior change
- Updated TOC and index files for proper discoverability
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| docs/core/compatibility/core-libraries/11/memorystream-max-capacity.md | New breaking change article documenting the MemoryStream maximum capacity enforcement and exception type change |
| docs/core/compatibility/toc.yml | Added TOC entry for the new breaking change article |
| docs/core/compatibility/11.md | Added index entry in the Core .NET libraries section |
docs/core/compatibility/core-libraries/11/memorystream-max-capacity.md
Outdated
Show resolved
Hide resolved
…acity.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
jozkee
reviewed
Jan 9, 2026
docs/core/compatibility/core-libraries/11/memorystream-max-capacity.md
Outdated
Show resolved
Hide resolved
…wing same exception Co-authored-by: jozkee <16040868+jozkee@users.noreply.github.com>
jozkee
approved these changes
Jan 9, 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.
MemoryStream Breaking Change Documentation
/home/runner/work/docs/docs/docs/core/compatibility/core-libraries/11/memorystream-max-capacity.mdArray.MaxLengthinstead of hardcoding0x7FFFFFC7OutOfMemoryExceptionandArgumentOutOfRangeExceptioncan still occur/home/runner/work/docs/docs/docs/core/compatibility/11.mdto include the new entry/home/runner/work/docs/docs/docs/core/compatibility/toc.ymlto include the new documentLatest update: Changed the alternative code example to use a
TrySetLengthhelper method that returns a boolean instead of throwing the same exception.Original prompt
This section details on the original issue you should resolve
<issue_title>[Breaking change]: Update MemoryStream max capacity</issue_title>
<issue_description>### [Breaking change]: MemoryStream maximum capacity updated and exception behavior changed
Description
Starting in .NET 11 Preview 1, the
System.IO.MemoryStreamclass has been updated to enforce a maximum capacity of0x7FFFFFC7bytes, which is the actual maximum length of a byte array supported by the CLR. Previously,MemoryStreamallowed capacities up toint.MaxValue(0x7FFFFFFF), which could result in anOutOfMemoryExceptionwhen attempting to allocate memory beyond the supported limit.Additionally, the exception behavior has changed when attempting to set a
MemoryStream's capacity or length beyond the new maximum. Instead of throwing anOutOfMemoryException, theMemoryStreamnow throws anArgumentOutOfRangeExceptionin these scenarios (this is the key difference).This change ensures that
MemoryStreambehaves consistently with the CLR's actual memory allocation limits and provides a more accurate exception type for invalid capacity or length values.Version
.NET 11 Preview 1
Previous behavior
MemoryStreamallowed capacities up toint.MaxValue(0x7FFFFFFF), which could result in anOutOfMemoryExceptionwhen attempting to allocate memory beyond the CLR's supported limit of0x7FFFFFC7.MemoryStreamto a value greater than the supported limit, anOutOfMemoryExceptionwas thrown.Example:
New behavior
MemoryStreamnow enforces a maximum capacity of0x7FFFFFC7bytes. Attempting to set the capacity or length beyond this limit will throw anArgumentOutOfRangeException.OutOfMemoryExceptiontoArgumentOutOfRangeException.Example:
Reason for change
This change was introduced to align
MemoryStream's behavior with the actual memory allocation limits of the CLR. The previous behavior allowed developers to specify capacities or lengths that exceeded the supported limit, leading to runtime failures with less descriptive exceptions (OutOfMemoryException). By capping the maximum capacity and throwingArgumentOutOfRangeException, the change improves runtime reliability and provides clearer feedback to developers.Recommended action
Developers should review any code that sets the capacity or length of a
MemoryStreamto ensure it does not exceed the new maximum of0x7FFFFFC7bytes.If your code was only catching
OutOfMemoryExceptionwhen working withMemoryStreamcapacity or length operations, you should update it to also catchArgumentOutOfRangeException. For example:You can also add a check before setting the capacity or length to avoid the exception:
Affected APIs
System.IO.MemoryStream.CapacitySystem.IO.MemoryStream.SetLength(long)System.IO.MemoryStream.ctor(int)Category
Associated WorkItem - 543464</issue_description>
Comments on the Issue (you are @copilot in this section)
@jozkee > #### Recommended action > > ```csharp > var stream = new MemoryStream(); > try > { > stream.SetLength(someLength); > } > catch (OutOfMemoryException) > { > // Handle out of memory scenario > } > catch (ArgumentOutOfRangeException) > { > // Handle invalid capacity/length scenario > } > ```@jkotas is it still possible that an OOMException arises from MemoryStream or can we safely suggest to fully replace it with ArgumentOutOfRangeException?</comment_new>
<comment_new>@jkotas
What would make it impossible?
new byte[....]can always throw an OutOfMemory exception if there is not enough memory available on the machine.It de...
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Internal previews