I used DotMemory to Profile @Rana_Ian solution, and I called GC to enforce full collection.
I found that large Streams will stuck in LOH!
And after adding one extra line
public static void Clear(MemoryStream ms)
{
var buffer = ms.GetBuffer();
Array.Clear(buffer, 0, buffer.Length);
ms.Position = 0;
ms.SetLength(0);
ms.Capacity = 0; // <<< this one ******
}
I clicked on F12 to see Capacity
Implementation, And I found this (I simplified generated code a little bit - I'm using Resharper):
public virtual int Capacity
{
get
{ .... // some code }
set
{
if ((long) value < this.Length) { // throw some ex }
if (!this._isOpen) { // some another code }
if (!this._expandable && value != this.Capacity) { //MemoryStreamNotExpandable }
if (!this._expandable || value == this._capacity) return;
if (value > 0)
{
byte[] numArray = new byte[value];
if (this._length > 0)
Buffer.InternalBlockCopy((Array) this._buffer, 0, (Array) numArray, 0, this._length);
this._buffer = numArray;
}
else
this._buffer = (byte[]) null; /// <<<< that's it! I need this one
this._capacity = value;
}
}
So It's clearing the Buffer, I'm not sure if this is correct or not!