Sanath Shetty
Sanath Shetty

Reputation: 484

Reading a filestream using too much memory

I want to read a file into to stream as a base64string and I am using the below code to read the file , but when i see the memory usage is shooting over 900MB, when I monitor it through the task Manager. am I missing something, I could see that the memory usage shoots up when I read it into the string ,(I have used a 150 MB file to test the same).

stringBuilder Sb=  new stringBuilder ();
using (var fs = new FileStream(@"C:\Users\Sanath Kumar\Desktop\s.pdf", FileMode.Open, FileAccess.Read))
{
    var buffer = new byte[fs.Length];
    fs.Read(buffer, 0, (int)fs.Length);
    Sb.Append(Convert.ToBase64String(buffer));
    buffer = null;
    fs.Flush();
    fs.Dispose();
}

Upvotes: 0

Views: 983

Answers (1)

JaredPar
JaredPar

Reputation: 754685

The problem here is you are reading the file in it's entirety into memory and duplicating the data several times.

  • The byte[] array
  • The StringBuilder which has a char[] backing array

If this file is of a significant size this will definitely cause a spike in memory.

Instead of reading it all in at once try using the ToBase64Transform class instead. This is designed to work with a Stream as input and will avoid loading the entire file into memory at one time. Instead it will process it in chunks.

Upvotes: 5

Related Questions