Good !!
I have a Stream of a ZIP file of approx. 450 Mb, and I need to convert it to an array of bytes. To do this, MemoryStream (System.IO.MemoryStream) is used by default, it follows the code I used:
Stream receiveStream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
receiveStream.CopyTo(ms);
byte[] dadosArquivo = ms.ToArray();
}
return dadosArquivo;
The problem is that when using the CopyTo method, an exception of type OutOfMemoryException occurs. From the tests I've done, the MemoryStream limitation is approx. 256 Mb of Stream size.
Some extra info:
- This Stream I get via response from an Http request (HttpWebResponse);
- I use MemoryStream to do this parse, because it was the only form I found in my searches.
- In relation to memory, I'm using a machine with 6Gb of RAM, I did the same test on another 8Gb machine and the limitation of MemoryStream is the same.
The error StackTrace follows:
System.OutOfMemoryException was caught HResult=-2147024882
Message=Exceção do tipo 'System.OutOfMemoryException' foi acionada.
Source=mscorlib
StackTrace:
em System.IO.MemoryStream.set_Capacity(Int32 value)
em System.IO.MemoryStream.EnsureCapacity(Int32 value)
em System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
em System.IO.Stream.InternalCopyTo(Stream destination, Int32 bufferSize)
em System.IO.Stream.CopyTo(Stream destination)
em HiperPdvLibrary.Integracao.Api.ApiRequest.GetByteRequest(HttpStatusCode& status)
InnerException:
I wonder if anyone has ever had this situation or do you have any other suggestions for doing this conversion, maybe doing this process in parts?
Hugs