-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Document MemoryStream maximum capacity breaking change in .NET 11 #50992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…NET 11 Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
BillWagner
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM @gewarren
Let's ![]()
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>
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