RecyclableMemoryStream, a better MemoryStream

More than 5 years after my first discovery, RecyclableMemoryStream is still relevant. With a clever use of buffer chains, this class should be used in (any) situation where a MemoryStream could be used.

This project has evolved to take into account the new memory efficiency paradigms integrated into the latest versions of .Net Core (Span, ReadOnlySpan, ReadOnlySequence, and Memory). It is more useful than ever to reconcile throughput and CPU load.

From Github project’s

 “In particular it is optimized to do the following:

  • Eliminate Large Object Heap allocations by using pooled buffers
  • Incur far fewer gen 2 GCs, and spend far less time paused due to GC
  • Avoid memory leaks by having a bounded pool size
  • Avoid memory fragmentation
  • Allow for multiple ways to read and write data that will avoid extraneous allocations
  • Provide excellent debuggability and logging
  • Provide metrics for performance tracking