1

Here's my code to read a text file, which happens to be larger than 1GB and is pipe-delimited:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string strFilePath = @"C:\Users\Me\Desktop\123.txt";

        private void button1_Click(object sender, EventArgs e)
        {
            var arrRawData = File.ReadLines(strFilePath).Select(line => line.Split('|')).ToArray();
        }
    }
}

When I click the button I get this error:

"System.OutOfMemoryException' occurred in mscorlib.dll"

I have 16GB of RAM...what am I doing wrong?

Soner Gönül
  • 94,086
  • 102
  • 195
  • 339
MrPatterns
  • 4,044
  • 26
  • 61
  • 83

3 Answers3

9

Your program is operating in 32-bit mode. Which will never permit reading more than about 650 megabytes of data, about the largest size hole available in the address space.

Project + Properties, Build tab, untick the "Prefer 32-bit" option.

Do note that using this much memory is still unnecessary in almost any app that parses file data. Just read one line at a time with the StreamReader class.

Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455
  • 1
    "using this much memory is still unnecessary", oh, I don't know ;p I've written plenty many things that make use of all that system memory – Marc Gravell Jan 10 '13 at 21:06
  • I unticked "Prefer 32-bit" option. When running the program I now get the error: "Error while trying to run project: Unable to start program'(path of program'\WindowsFormsApplication1.exe'. Unrecognized error occurred in the Windows Web Services framework. When I re-tick "Prefer 32-bit" the program runs okay (albeit before I click the button). – MrPatterns Jan 10 '13 at 21:41
  • I'm choosing this answer because it mentions using StreamReader instead. – MrPatterns Jan 10 '13 at 22:02
1

there is no limit imposed by visual studio. you may have 16G but you cannot use all of it for your program. try using a stream or not reading the whole file at the same time but rather line by line...

Z.D.
  • 12,393
  • 1
  • 32
  • 54
0

When running your program error occures: "Error while trying to run project: Unable to start program. Unrecognized error occurred in the Windows Web Services framework.

the solution is turn off your firewall. Firewall bloks:

Event: Traffic
IP Address/User: 0000:0000:0000:0000:0000:0000:0000:0001
Description: Microsoft Visual Studio 2012 (devenv.exe)
Path: C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe

Message: Blocked Outgoing TCP - Source 0000:0000:0000:0000:0000:0000:0000:0001 :  (12276)  Destination 0000:0000:0000:0000:0000:0000:0000:0001 :  (12275)
Saharsh Shah
  • 27,975
  • 8
  • 43
  • 82
Cyrus
  • 2,033
  • 2
  • 18
  • 35