Size of Byte Array for big files?

  • Thread starter Thread starter Anil Gupte
  • Start date Start date
A

Anil Gupte

I am using the following:
Dim fsReadStream As New FileStream(TextBoxVideoFileName.Text, FileMode.Open,
FileAccess.Read)
Dim brReader As New BinaryReader(fsReadStream)
Dim ByteArray() As Byte

ByteArray = brReader.ReadBytes(fsReadStream.Length)

This works fine for smaller files, but crashes with an overflow exception
with a large file (2.5GB). Is the size of the array a problem? How can I
change it? Any other solution?

Thanx,
 
AFAIK arrays are indexed using an integer whose max value allows for 2 Go.

Even for smaller files it's likely better to avoid loading a whole file in
memory unless this is strictly needed and especially if those files are not
limited in size. A common practice is to read a file in "chunks" so that you
consume only the size of a buffer that holds the current "access window" on
file data...

So it would depend what you are doing once the whole file is loaded in
memory....
 
This works fine for smaller files, but crashes with an overflow
exception with a large file (2.5GB). Is the size of the array a
problem? How can I change it? Any other solution?

Yes, you're creating a 2.5GB array in memory. I believe there is a 2GB
limit?

You should stream the file using a smaller array as data is needed.
 
Back
Top