-3

I try to use File.ReadAllBytes(FilePath) but it throw Exception of type 'System.OutOfMemoryException'

anybody can help me

Xaqron
  • 29,931
  • 42
  • 140
  • 205
mariosoft1985
  • 45
  • 2
  • 6
  • 1
    Show your full work please. – Soner Gönül Feb 08 '14 at 11:54
  • 1
    How big is file you're trying to read? – cichy Feb 08 '14 at 11:56
  • 1
    the file may be more than 5GB – mariosoft1985 Feb 08 '14 at 12:04
  • Do not read the file in one go. Read it in blocks (using FileStream.Read or similar) and process those as you go. If you need more specific advise, please tell us a bit more about what you want to *do* with the data in the file. – Paul-Jan Feb 08 '14 at 12:14
  • I have another problem with concat two byte[] on of them have more than 300,000,000 byte , it's make **Exception of type 'System.OutOfMemoryException' was thrown** i use this code : byte[] b3 = by2.Concat(by1).ToArray(); – mariosoft1985 Feb 08 '14 at 13:25

1 Answers1

0
using (var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) 
{ 
   byte[] buffer = new byte[fs.Length];
   int bytesRead = fs.Read(buffer, 0, buffer.Length);
} 

The above works well:

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • This will not work for a 5GB file. – JLRishe Feb 08 '14 at 12:11
  • yes it doesn't work for 5GB file !! – mariosoft1985 Feb 08 '14 at 12:31
  • I think there will be no exact solution, as at some point of time, the system will eventually run out of memory. Another thing to do is read chunks of data using some interval so that memory can be claimed back – Amit Joki Feb 08 '14 at 12:39
  • I have another problem with concat two byte[] on of them have more than 300,000,000 byte , it's make **Exception of type 'System.OutOfMemoryException' was thrown** i use this code : byte[] b3 = by2.Concat(by1).ToArray(); – mariosoft1985 Feb 08 '14 at 13:24