PooledMemoryStreamOptions Class

Configures the buffer sizes and pooling behavior used by PooledMemoryStream.

public sealed class PooledMemoryStreamOptions

Remarks

To benefit from pooling, the stream only ever rents byte arrays in the discrete sizes listed in PooledMemoryStreamOptions.BufferSizes (or, when a contiguous buffer larger than the largest tier is required, a multiple of the largest tier). Arrays of arbitrary sizes are never rented, which keeps the shared pool buckets small and reusable.

An instance becomes immutable (frozen) the first time it is used to create a PooledMemoryStream; the PooledMemoryStreamOptions.Default instance is always frozen. Setting any property on a frozen instance throws an InvalidOperationException. Configure all properties before passing the options to a stream.

Constructors

NameDescription
PooledMemoryStreamOptions()

PooledMemoryStreamOptions()

PooledMemoryStreamOptions.cs
public PooledMemoryStreamOptions()

No documentation is available.

Properties

NameDescription
BufferSizesGets or sets the discrete pooled buffer sizes, in bytes, in strictly ascending order (for example [4096, 131072, 1048576, 10485760]). Small streams use the smallest size and grow through the larger sizes; the largest size is used for big streams, and contiguous buffers larger than it round up to a multiple of it. Must contain at least one size. Defaults to [4096, 65536, 1048576].
ClearOnReturnGets or sets a value indicating whether buffers are cleared (zeroed) when returned to the pool. This is slower but ensures previously written data cannot be observed by another stream that later rents the same array. Defaults to false.
DefaultGets the default options instance (4 KiB / 64 KiB / 1 MiB tiers). This instance is frozen (immutable) and is shared across all streams that don't specify custom options.
IsFrozenGets a value indicating whether this instance has been frozen and can no longer be modified.
MaxRetainedBytesPerBucketGets or sets the maximum number of bytes retained by the shared pool for each distinct array size. When a returned buffer would exceed this limit, it is left for the garbage collector instead. Defaults to 16 MiB.

Gets or sets the discrete pooled buffer sizes, in bytes, in strictly ascending order (for example [4096, 131072, 1048576, 10485760]). Small streams use the smallest size and grow through the larger sizes; the largest size is used for big streams, and contiguous buffers larger than it round up to a multiple of it. Must contain at least one size. Defaults to [4096, 65536, 1048576].

public ImmutableArray<int> BufferSizes { get; set; }

Property value

ImmutableArray<int>

Gets or sets a value indicating whether buffers are cleared (zeroed) when returned to the pool. This is slower but ensures previously written data cannot be observed by another stream that later rents the same array. Defaults to false.

public bool ClearOnReturn { get; set; }

Property value

bool

Remarks

The members bounded by PooledMemoryStream.Length never expose unwritten bytes, and PooledMemoryStream.GetBuffer zeroes the region past PooledMemoryStream.Length before handing the array out, so it cannot surface data left by an unrelated stream either. The one region that stays uninitialized is the span returned by the IBufferWriter<T> members, which is the usual contract for a buffer writer.

The pool is process-wide and clearing happens on return, so this setting governs the data this stream hands back to other streams. It does not clean the arrays this stream rents from them.

Gets the default options instance (4 KiB / 64 KiB / 1 MiB tiers). This instance is frozen (immutable) and is shared across all streams that don't specify custom options.

public static PooledMemoryStreamOptions Default { get; }

Gets a value indicating whether this instance has been frozen and can no longer be modified.

public bool IsFrozen { get; }

Property value

bool

MaxRetainedBytesPerBucket

PooledMemoryStreamOptions.cs

Gets or sets the maximum number of bytes retained by the shared pool for each distinct array size. When a returned buffer would exceed this limit, it is left for the garbage collector instead. Defaults to 16 MiB.

public long MaxRetainedBytesPerBucket { get; set; }

Property value

long