Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting out of memory exception while reading via binary reader and then converting to string while reading a large file.

I increased the size to higher value but on converting to string issue occurs.

What I have tried:

FileStream fileStream = new FileStream(strFilePath + strFileName, FileMode.Open, FileAccess.Read, FileShare.Read);
              int num = 0;
              BinaryReader binaryReader = new BinaryReader(fileStream, Encoding.GetEncoding(37));
              byte[] array = new byte[4];
              //byte[] array = new byte[1024];
              int num2 = 0;
              array = binaryReader.ReadBytes(4);
              //long length = fileStream.Length;

              num += 4;
              Array.Reverse(array);
              int i = BitConverter.ToInt32(array, 0);
              int num4 = 0;
              bool flag = false;
              bool flag2 = false;
              bool flag3 = false;
              bool flag4 = false;
              bool flag5 = false;
              bool flag6 = false;
              bool flag7 = false;
              bool flag8 = false;
              bool flag9 = false;
              bool flag10 = false;


              long num5 = 0L;



              string line;

              // Read the file and display it line by line.



              while (i > 0)
              {
                  byte[] array2 = new byte[i + 1];
                  //byte[] array2 = new byte[fileStream.Length];
                  array2 = binaryReader.ReadBytes(i);
                  num5 += (long)(i + 4);
                  num += i;
                  string @string = Encoding.ASCII.GetString(array2);
                  string key;
Posted
Updated 17-Jan-17 19:59pm

1 solution

Your code sample is incomplete, so it's difficult to say exactly what the problem might be - you don't show where i is changed for example.
But ... This comment suggests the problem:
// Read the file and display it line by line.

If you are adding each line to a control, then it's likely that the problem is control replaced, and that you are trying to show too much data at once. If you are adding it to a ListBox, or a DataGridView for example then you will run out of Handles long before you run out of memory, and that will trigger the same error message.

It's pointless just "throwing" that much data at your user anyway, so page it, filter it, search it - but just expecting the user to work with thousands of lines is just going to annoy them!

Remove the code thgthgat actually displays the lines, and I'm pretty sure the error message will go as well...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900