0

I am downloading a file using HTTP and then processing it and saving parts of it to e CosmosDB, the code runs fine in VS2017 but after testing in VS2019 I am getting out of memory exceptions and I cannot figure out why.

The code is fairly simple as follows:

 MemoryStream originalFileStream = new MemoryStream(content);

 using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
 {
    MemoryStream decompressedFileStream = new MemoryStream();
    decompressionStream.CopyTo(decompressedFileStream);
    byte[] fileResult = new byte[decompressedFileStream.Length];
    decompressedFileStream.Position = 0;
    decompressedFileStream.Read(fileResult, 0, fileResult.Length);
    string result = System.Text.Encoding.UTF8.GetString(fileResult);
}

The out of memory error happens in the CopyTo() operation, but since the exact same code works in VS2017 I am a bit stuck as how to fix this in VS2019

How can this be sorted out in VS2019?

Matt Douhan
  • 2,053
  • 1
  • 21
  • 40
  • Possible duplicate of [OutOfMemoryException while populating MemoryStream: 256MB allocation on 16GB system](https://stackoverflow.com/questions/15595061/outofmemoryexception-while-populating-memorystream-256mb-allocation-on-16gb-sys) – Mustafa Gursel Jun 24 '19 at 10:37
  • Avoid using memory streams (and any intermediate storage such as byte arrays or strings). Instead try to get the source and target as streams. (Example, if you are saving to disk use a FileStream as the target of the decompressionStream.CopyTo) – Magnus Jun 24 '19 at 10:54
  • I thought I was using a min of those I am not saving to disk, I am simply downloading a zip file decompressing it and making it a stringso I can make it into a JObject and parse it later – Matt Douhan Jun 24 '19 at 10:57
  • Same content? and how large it is? – shingo Jun 25 '19 at 03:19
  • file is 24 MB compressed, content is slightly different, maybe I am just hitting a limit? – Matt Douhan Jun 25 '19 at 03:21

0 Answers0