473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Out of Memory Error during reDim Statment

I've got a program that parses text files. The text files come to me
in Unicode and they contain goofy characters that VB chokes on -
treats them as eof markers. I have already been through this issue in
another thread and I have received an answer that works (code is
below)

However, my test data thus far has been in the 10-50MB size range. I
now have a case where the text file is 650MB in size!

I need to convert it to ANSI for my program to parse it correctly
using the line input method. There is over 1 million lines of text in
this file, and each line contains about 12 fields' worth of
information.

Here is the code:

Dim bHTML() as Byte
Dim myLong as Long
myLong = FreeFile
Open myPath & myName For Binary as #myLong
ReDim bHTML(1 to LOF(myLong))
Get #myLong, , bHTML
Close #myLong

Open myPath & "temp_" & myName for Output as #myLong
Print #myLong, bHTML
Close #myLong

When running this code against the 650MB file - I get an Out of Memory
error instantaneously. I am not very familiar with arrays, so do not
know what to do to work-around this type of error.

Does anyone have any suggestions?
Jul 17 '05 #1
4 7957

"Trevor Fairchild" <MR*******@e-crime.on.ca> wrote in message
news:f5**************************@posting.google.c om...
I've got a program that parses text files. The text files come to me
in Unicode and they contain goofy characters that VB chokes on -
treats them as eof markers. I have already been through this issue in
another thread and I have received an answer that works (code is
below)

However, my test data thus far has been in the 10-50MB size range. I
now have a case where the text file is 650MB in size!

I need to convert it to ANSI for my program to parse it correctly
using the line input method. There is over 1 million lines of text in
this file, and each line contains about 12 fields' worth of
information.

Here is the code:

Dim bHTML() as Byte
Dim myLong as Long
myLong = FreeFile
Open myPath & myName For Binary as #myLong
ReDim bHTML(1 to LOF(myLong))
If the file is 650 MB you are trying to allocate 650 MB of memory. You get
the error because you don't have that much memory availiable.

Try creating an array that your system can handle and step through the file
with the same array lengths. Try something like 1 MB. Access and transfer
the file in 1 MB portions until you have made it through the file. You can
do this using the Get #myLong, [File Byte Position], bHTML.

Just increment the [File Byte Position] in steps of the length of the array
you declare. Make sure when you get to the last segment you ReDim the bHTML
array to the remaining byte size (your file may not be exactly 650 MB)

Regards
Mike

Get #myLong, , bHTML
Close #myLong

Open myPath & "temp_" & myName for Output as #myLong
Print #myLong, bHTML
Close #myLong

When running this code against the 650MB file - I get an Out of Memory
error instantaneously. I am not very familiar with arrays, so do not
know what to do to work-around this type of error.

Does anyone have any suggestions?

Jul 17 '05 #2
This was what I was trying to do. However, I cannot get the ReDim
Statement to only take out 1,000,000 characters at a time.

I put in a loop to cycle through, using

ReDim bHTML(c, c + 1000000)

but it does not seem to work right - I keep on getting the first
1,000,000 characters, regardless of what c is equal to. It doesn't
make much sense to me, but I get the impression this isn't just
spaces, but array dimensions, and I'm out of my league in that.

The "Get #myLong, STARTING, bHTML" simply tells it where to start
getting the data, not where to end - so I just get the 1,000,000 - to
EOF, instead of all of it. Furthermore, the system crashes at the
ReDim statement - I have to redim in smaller chunks - that's my
problem, I guess.

I've tested a fair amount on the redim and cannot seem to figure it
out. The file is in Unicode, so each character is actually 2 bytes in
size, not 1 - I tried ReDim bHTML(2 to 4) and that gets what I would
expect - the first 2 characters in the file. When I ReDim bHTML(4 to
8) I still only get the first 2 characters in the file - I would
expect to get the 3rd & 4th characters.

I don't get it.
Jul 17 '05 #3
MR*******@e-crime.on.ca (Trevor Fairchild) wrote:
I've got a program that parses text files. The text files come to me
in Unicode and they contain goofy characters that VB chokes on -
treats them as eof markers. I have already been through this issue in
another thread and I have received an answer that works (code is
below)

However, my test data thus far has been in the 10-50MB size range. I
now have a case where the text file is 650MB in size!

I need to convert it to ANSI for my program to parse it correctly
using the line input method. There is over 1 million lines of text in
this file, and each line contains about 12 fields' worth of
information.


Air code - you'll need to make adjustments ;-)
It's based on a compiled DOS Basic program from 1991 that read 300MB of trashed
UNIX file system in 32K chunks to recover email.

Dim FileSize as long
Dim CurrentPosition as long
Dim MyArray(1000000) as Byte
Dim LastBytes as Long

Open the file and use LOF to get the FileSize.

LastBytes = FileSize mod 1,000,000

CurrentPosition = 0

Do While CurrentPosition < FileSize

Get MyArray from files, starting at CurrentPosition. 'read in 1,000,000 bytes

Increment CurrentPosition by 1,000,000 'move pointer to next block of bytes

Process 1,000,000 bytes in MyArray.
End Do

Get MyArray from files, starting at CurrentPosition.

Process LastBytes of valid data in MyArray

No ReDim, no running out of memory.

If you have enough available memory, MyArray could be 5 or 10 million.
More about me: http://www.jecarter.com/
VB3/VB6/C/PowerBasic source code: http://www.jecarter.com/programs.html
Freeware for the Palm with NS Basic source code: http://nsb.jecarter.com
Drivers for Pablo graphics tablet and JamCam cameras: http://home.earthlink.net/~mwbt/
johnecarter at@at mindspring dot.dot com. Fix the obvious to reply by email.
Jul 17 '05 #4

"Trevor Fairchild" <MR*******@e-crime.on.ca> wrote in message
news:f5**************************@posting.google.c om...

You are a bit confused about what redimming an array does. It has no
affect on which characters are read from the input file. Whether you
number four elements as (0 to 3) or (1 to 4) or even (123 to 126) makes
no difference - they are still just four elements, and the first time
you read, you will get the first four items.

Your original program is basically reading two byte unicode characters
as individual bytes, then implicitly converting the byte array to an
ANSI string by using the Print statement to output them. As long as you
read an even number of bytes each time, it works just as well in blocks.

Here is an example of how to go about reading and writing a file of any
size in blocks of 16384, a reasonable power of 2 size for doing file IO
stuff:

Private Sub Convert(Path As String, FileName As String)
Dim bHTML() As Byte
Dim nFileIn As Long
Dim nFileOut As Long
Dim nBytes As Long
Const BlockSize As Long = 16384
Dim nBlocks As Long
Dim nExtra As Long
Dim nCnt As Long

nFileIn = FreeFile
Open Path & FileName For Binary As #nFileIn

nBytes = LOF(nFileIn)
nBlocks = nBytes \ BlockSize
nExtra = nBytes Mod BlockSize

nFileOut = FreeFile
Open Path & "temp_" & FileName For Output As #nFileOut

ReDim bHTML(1 To BlockSize)

For nCnt = 1 To nBlocks
Get #nFileIn, , bHTML
Print #nFileOut, bHTML
Next n

If nExtra > 0 Then
ReDim bHTML(1 To nExtra)
Get #nFileIn, , bHTML
Print #nFileOut, bHTML
End If

Close #nFileOut
Close #nFileIn

End Sub
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tmenke | last post by:
Hello, I was wondering if anyone can help. I wrote a program in VBA. It does some calculations with matrices (arrays) . These arrays are dynamic and are sized according to a user defined input....
1
by: Stu | last post by:
Hi, Im reading a file in from disk as a byte array then passing it to a memory stream for decryption using crypto api functions. What I have found is that you need to reduce the array length by 2...
1
by: Ben | last post by:
I have written a procedure which calls the CORREL function of Excel to run correlation analysis on two arrays, then populate a table with the resulting correlation coefficient. This process loops...
0
by: Jon | last post by:
I'm puzzled.. let's say I have an array of streamwriters. a lot.. 'the code is something like dim sw() as streamwriter redim sw(10000) 'instantiate for i as int16 = 0 to 9999 sw(i) = new...
6
by: Ken | last post by:
When running a program in the debugger, what would cause it to crash without any error messages? I get "The program has exited with code 0 (0x0)". The program is a MDI app with threading for...
2
by: James | last post by:
Hi, I need help with a couple of questions, I have create and structure like: Structure byteArray Public byteA() As Byte End Structure Public ptrArray() As byteArray
3
by: Doug Durrett | last post by:
I'm having an issue and wanted to pass it by everyone to see what you think. Here is my code. //Code Start searchs = new...
5
by: Al G | last post by:
Hi, I'm converting a bit of POP3 VB6 code to VB2005, and have run into this error with the following code. Can someone help me find out what I'm missing/doing wrong? 'holds the attachments...
8
by: ozarka | last post by:
I am getting "out of memory" (run time error 7) error on ReDim Preserve myArr(totalAmount). When I put my mouse over this line it reads 65894. My text file has 780k lines (almost a meg.). Anything I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.