Search code examples
c#streambinaryreader

Byte[stream.length] - out of memory exception, best way to solve?


i am trying to read the stream of bytes from a file. However when I try to read the bytes I get a

The function evaluation was disabled because of an out of memory exception

Quite straightforward. However, what is the best way of getting around this problem? Is it too loop around the length at 1028 at a time? Or is there a better way?

The C# I am using

BinaryReader br = new BinaryReader(stream fs);

// The length is around 600000000
long Length = fs.Length;

// Error here
bytes = new byte[Length];

for (int i = 0; i < Length; i++)
{
   bytes [i] = br.ReadByte();
}

Thanks


Solution

  • Well. First of all. Imagine a file with the size of e.g. 2GB. Your code would allocate 2GB of memory. Just read the part of the file you really need instead of the whole file at once. Secondly: Don't do something like this:

    for (int i = 0; i < Length; i++)
    {
       bytes [i] = br.ReadByte();
    }
    

    It is quite inefficient. To read the raw bytes of a stream you should use something like this:

    using(var stream = File.OpenRead(filename))
    {
        int bytesToRead = 1234;
        byte[] buffer = new byte[bytesToRead];
    
        int read = stream.Read(buffer, 0, buffer.Length);
    
        //do something with the read data ... e.g.:
        for(int i = 0; i < read; i++)
        {
            //...
        }
    }