0

I don't read big data text file. My file approx size 2GB. However, themaximum file size I read was 200MB. This How to do this in C#. Help me guys.

private void button12_Click(object sender, EventArgs e)
{
        String file = textBox1.Text;

        if (string.IsNullOrEmpty(file))
        {
            MessageBox.Show("Файлыг заана уу!!");
        }

        else
        {  
            System.IO.FileInfo fi = new System.IO.FileInfo(file);
            System.IO.FileStream fs = new System.IO.FileStream(file, System.IO.FileMode.Open);
            int bufferSize = 500000000;  // file size

            using (System.IO.BufferedStream bs = new System.IO.BufferedStream(fs, bufferSize))
            {
                byte [] buffer = new byte [bufferSize];

                int readLength;
                do
                {
                    readLength = bs.Read(buffer, 0, bufferSize);
                    richTextBox1.Text += System.Text.Encoding.ASCII.GetString(buffer);
                    Application.DoEvents();

                } while (readLength == bufferSize);

                bs.Close();
            }
            fs.Close();
        }
        String names = richTextBox1.Text;

}

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
dashka
  • 29
  • 6
  • What happens when you trying to read the data? Did you get an exception? – Mighty Badaboom Apr 02 '19 at 09:41
  • 1
    use [File.ReadLines](https://learn.microsoft.com/ru-ru/dotnet/api/system.io.file.readlines?view=netframework-4.7.2) – vasily.sib Apr 02 '19 at 09:42
  • When reading 600MB files, the System.OutOfMemoryException error occurred. I know this is a memory problem. Do you have any idea about this? – dashka Apr 02 '19 at 09:45

1 Answers1

0

You are wasting memory here with the following line:

richTextBox1.Text += "some_string";

Strings are immutable, so each time you do this, you are allocating new memory the size of the existing Text string, plus the concatenated string, and throwing the old string away. The garbage collector will reclaim this memory, but it won't necessarily do it fast enough to avoid exhausting the process' available memory.

At a minimum, try instead building up this string with StringBuilder and assigning to the textbox once, after the loop.

You may well still encounter memory exhaustion however, as a 2GB string is still loads in the context of displaying text in a window.

Tom W
  • 5,108
  • 4
  • 30
  • 52