Link to home
Start Free TrialLog in
Avatar of Frans_Truyens
Frans_TruyensFlag for Belgium

asked on

Regex Out of memory on 32 bit Windows

I am using Regex in VB.NET on a 32 bit Windows 7 to read large files, the largest being 2,2 Gigabyte. I am reading the files with:
strText = File.ReadAllText(BigFile, System.Text.Encoding.ASCII)

The problem is that I am getting an out of memory error when reading the files. On smaller files everything works fine

How can I solve that?
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

I'm not a vb.net programmer, but your problem will be solved for sure using stream to read file. As in other languages, load the whole file in memory works fine, as you said, for small files: for large files you have to open them in a stream and process line-by-line. I found this code which maybe could give you an idea:
       Dim inputfile As String = "C:\BIGFILE.txt"
        Dim count As Int32 = 0
        Dim lineoftext As String = ""

        If File.Exists(inputfile) Then
            Dim _read As New StreamReader(inputfile)
            Try
               Do While (_read.peek() >= 0)
                    lineoftext = _read.ReadLine()
                     ' use regex here against lineoftext
                    count += 1
                End While

                Console.WriteLine("Total Lines in " & inputfile & ": " & count)
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            Finally
                _read.Close()
            End Try
        End If

Open in new window

Hope this helps :-)
Avatar of Frans_Truyens

ASKER

Won't that be too slow on a 2,2 Giga file?
On the contrary, stream has been created right to access large files: what it should be slow it would be load the whole file in memory, which in your case gives an out-of-memory error. It's obvious that if you have to access a 2.2Gb text file, you can't expect to do it in a few milliseconds like a small file, but using streams reduce the time needed to process the file. I used them in Delphi, and I don't know another technique to access large files other than the use of read stream/ write stream procedures...
Unless you can't change the file's creation, deciding for instance to create 100 small files instead of 1 large file (but this would be possible only if the file has been createdd by your same program :-)
OK. I will try this afternoon. I don't really have another choice I think. Splitting the big file in parts, is not that easy. So I will try this afternoon.
ASKER CERTIFIED SOLUTION
Avatar of Marco Gasi
Marco Gasi
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks