Class MemoryStreamSlim
MemoryStreamSlim is a memory stream that is designed to be more memory efficient, and often times faster than the standard MemoryStream class. For relatively small size streams, this utilizes a specialized pool of small buffers for the backing memory, which can reduce the number of allocations and the amount of memory used; and for larger streams, it uses a list of re-usable buffers to store the contents of the stream that are allocated by an internally managed buffer pool that is designed to minimize GC memory traffic pressure, by allocating directly from the large object heap, and re-using buffers as much as possible.
This is only designed to replace MemoryStream use cases where the stream does not wrap an existing specific byte array. If an existing byte array buffer is provided on creation, that array will be used as the backing store, and the MemoryStreamSlim will simply wrap a standard MemoryStream to provide the same functionality as the standard MemoryStream.
public abstract class MemoryStreamSlim : MemoryStream, IAsyncDisposable, IDisposable
- Inheritance
-
MemoryStreamSlim
- Implements
- Inherited Members
Remarks
Since this operates on in-memory buffers, the asynchronous methods are not truly asynchronous, but they are provided for compatibility with the Stream class.
Fields
- StandardAsyncCopyBufferSize
The size of the buffer used for asynchronous copying. This is just actually the size we will use for each partial async write operation as we move through the list of internal buffers.
Properties
- CanRead
Gets a value indicating whether the current stream supports reading.
- CanSeek
Gets a value indicating whether the current stream supports seeking.
- CanWrite
Gets a value indicating whether the current stream supports writing.
- Capacity
Gets or sets the number of bytes allocated for this stream.
- CapacityLong
Gets or sets the number of bytes allocated for this stream as a long integer.
- GlobalDefaultSettings
Gets the default settings for MemoryStreamSlim instances.
- GlobalMaximumCapacity
Gets or sets the global maximum capacity for all instances of MemoryStreamSlim. This value affects newly created instances of MemoryStreamSlim only, and any existing instances will use the value that was set at the time they were created.
- Id
A unique identifier for this stream instance.
- InternalUseNativeLargeMemoryBuffers
Internal helper to get the value of the global setting for whether to use native memory buffers for large memory, this also shortcuts the check for the global settings by checking if this is running in a browser environment.
- Mode
Gets the operating mode of this stream instance.
- Settings
The settings for this stream instance.
- UseNativeLargeMemoryBuffers
Gets or sets the global setting for whether to use native memory buffers for large memory segments.
Methods
- AdjustActiveStreamCount(bool)
Helper to track the number of active MemoryStreamSlim instances.
- CopyToAsync(Stream, int, CancellationToken)
Asynchronously reads all the bytes from the current stream and writes them to another stream, using a specified buffer size and cancellation token.
- CopyToSyncAsAsync(Stream, int, CancellationToken)
For known destination classes that are in-memory, we can optimize the copy operation by copying synchronously instead of asynchronously.
- Create()
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized to zero.
- Create(MemoryStreamSlimOptions)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized to zero, and options to configure the stream instance.
- Create(Action<MemoryStreamSlimOptions>)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized to zero, and a delegate to set up options for the stream instance.
- Create(byte[])
Creates a new non-resizable instance of the MemoryStreamSlim class based on the specified byte array.
- Create(byte[], bool)
Creates a new non-resizable instance of the MemoryStreamSlim class based on the specified byte array with the CanWrite property set as specified.
- Create(byte[], int, int)
Creates a new non-resizable instance of the MemoryStreamSlim class based on the specified region (index) of a byte array.
- Create(byte[], int, int, bool)
Creates a new non-resizable instance of the MemoryStreamSlim class based on the specified region of a byte array, with the CanWrite property set as specified.
- Create(byte[], int, int, bool, bool)
Creates a new non-resizable instance of the MemoryStreamSlim class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer() set as specified.
- Create(int)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized as specified.
- Create(int, MemoryStreamSlimOptions)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized as specified, and options to configure the stream instance.
- Create(int, Action<MemoryStreamSlimOptions>)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized as specified, and a delegate to set up options for the stream instance.
- Create<TState>(Action<MemoryStreamSlimOptions, TState>, TState)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized to zero, and a delegate to set up options for the stream instance.
- Create<TState>(int, Action<MemoryStreamSlimOptions, TState>, TState)
Creates a new instance of the MemoryStreamSlim class with an expandable capacity initialized as specified, and a delegate to set up options for the stream instance.
- FlushAsync(CancellationToken)
Asynchronously clears all buffers for this stream, and monitors cancellation requests.
- GetBuffer()
Returns the array of unsigned bytes from which this stream was created.
- ReadAsync(byte[], int, int, CancellationToken)
Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
- ReadAsync(Memory<byte>, CancellationToken)
Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into
destination
, advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.
- ReleaseMemoryBuffers()
Requests that all large memory buffers managed by MemoryStreamSlim instances should be released back to the system as soon as possible.
- SetGlobalDefaultSettings(MemoryStreamSlimOptions)
Configures the default settings for MemoryStreamSlim instances based on the specified options.
- SetGlobalDefaultSettings(Action<MemoryStreamSlimOptions>)
Configures the default settings for MemoryStreamSlim instances based on the specified options configured by the setup delegate.
- SetGlobalDefaultSettings<TState>(Action<MemoryStreamSlimOptions, TState>, TState)
Configures the default settings for MemoryStreamSlim instances based on the specified options configured by the setup delegate.
- ToString()
Returns a string that represents the current object.
- WriteAsync(byte[], int, int, CancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
- WriteAsync(ReadOnlyMemory<byte>, CancellationToken)
Asynchronously writes the sequence of bytes contained in
source
into the current memory stream, advances the current position within this memory stream by the number of bytes written, and monitors cancellation requests.