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
  • 97,193
  • 102
  • 206
  • 364
MrPatterns
  • 4,184
  • 27
  • 65
  • 85

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
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 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 .
  • 12,657
  • 1
  • 31
  • 56
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
  • 28,687
  • 8
  • 48
  • 83
Cyrus
  • 2,261
  • 2
  • 22
  • 37