0
Good!!
I have a ZIP file stream with a size of approx. 450 Mb, and I need to convert it to an array of bytes. For this, the Memorystream (System.IO.Memorystream) is used by default, following the code I used:
Stream receiveStream = response.GetResponseStream();
using (MemoryStream ms = new MemoryStream())
{
receiveStream.CopyTo(ms);
byte[] dadosArquivo = ms.ToArray();
}
return dadosArquivo;
The problem, that when using the Copyto method, occurs a type exception Outofmemoryexception. From the tests I did, the Memorystream limitation is approx. 256 Mb of Stream size.
Some extra information:
- This Stream I receive via replay of an Http request (Httpwebresponse);
- I use Memorystream to do this parse, because it was the only way I found in my research.
- Regarding memory, I am using a machine with 6Gb of RAM memory, I did the same test on another machine of 8Gb and the limitation of Memorystream is the same.
Follow the Stacktrace error:
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 would like to know if anyone has already experienced this situation or has any other suggestion to make this conversion, perhaps to make this process by part?
Hugs
When you put relevant parts of the code, it is difficult to identify what you are doing with 2 lines. Have you done tests with more available memory? Have you checked the memory at this time? Have you tried segment copying instead of copying everything at once? https://msdn.microsoft.com/en-us/library/dd783870(v=vs.110). aspx
– Maniero
What is the source of the stream? And' a disk file? What do you want to do with the file after having it in memory?
– dcastro
Places the stacktrace also to make it easier to identify the error
– Pedro Laini
I put some more information that might clarify the problem.
– rafaeldals
You know a priori what will be the size of the file you will receive via HTTP? Or it is variable?
– dcastro