.Net Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - More.Net Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 26th, 2012, 05:00 PM
NeoScyther NeoScyther is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 12 NeoScyther User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 32 m 26 sec
Reputation Power: 0
[VB.NET] Getting an Out Of Memory exception when opening large files.

I'm working on a program that will take a file, grab the hex values for a certain number of bytes, and put them into a string. I dug around a bit and found this line of code somewhere:

rtbInp.Text = String.Join(" ", IO.File.ReadAllBytes(<insert file name>).Select(Function(b) b.ToString("X2")).ToArray())

Turns out, that worked perfectly, as it took each byte in the file, turned it into hex, and put it into the text box. My only alteration was to put it into a variable instead so I could later use SubString to grab specific values. Then I hit one minor problem: files larger than about 150MB. It wouldn't be often that I'd have files this large, but when I attempt to use one that big, I get "Exception of type 'System.OutOfMemoryException' was thrown". I've done some searching and have seen a couple sites that talk about file paging, but when looking at the code, it just confuses the heck out of me.

Basically, I'm looking for one of two things: a way to read the entire file, regardless of size, and avoid the out of memory exception, or a way to take the line of code above, but only have it read in a certain number of bytes (300 would be the most I'd need) while keeping the same functionality.

Any help would be greatly appreciated.

Reply With Quote
  #2  
Old April 28th, 2012, 10:51 AM
f'lar's Avatar
f'lar f'lar is offline
ASP.Net MVP
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 4,378 f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 4 m 57 sec
Reputation Power: 1509
Send a message via Google Talk to f'lar
Rather than using File.ReadAllBytes(), you'll need to open a streamreader and read the file in a few bytes at time... say 1024 byte chunks.

Each time your read a chunk perform your hex conversion, and then append the results to a StringBuilder object. Do no use string concatenation.

The other is to remember you are representing each byte of that large file with two characters, and each character is itself two bytes because of unicode, so a 150MB file on disk will use 600MB of RAM when you read it in. Are you sure you won't be better off leaving this file on disk and seeking to only the parts you need?
__________________
Primary Forum: .Net Development
Holy cow, I'm now an ASP.Net MVP!

[Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

http://twitter.com/jcoehoorn

Last edited by f'lar : April 28th, 2012 at 10:53 AM.

Reply With Quote
  #3  
Old April 28th, 2012, 05:03 PM
NeoScyther NeoScyther is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 12 NeoScyther User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 32 m 26 sec
Reputation Power: 0
Well, I initially had code to read in a character at a time which almost worked, but I think I read it in wrong or something. I think I had it get the ASCII value of each character, which gave major issues when that value hit higher than 127, since I was going from ASCII to Hex (yeah, I question my logic on that one), ASCII values above 127 didn't have Hex values that I saw, so I kind of screwed myself.

I'll take a look at it down the road, but in the meantime I did find a small workaround: I used a Try...Catch statement to catch the error, and if the user opens the file in a Hex Editor and deletes any data after a certain byte, it works just fine (only the first fifty or sixty bytes are actually needed).

Reply With Quote
  #4  
Old April 30th, 2012, 07:06 PM
f'lar's Avatar
f'lar f'lar is offline
ASP.Net MVP
Dev Shed Specialist (4000 - 4499 posts)
 
Join Date: Aug 2003
Location: WI
Posts: 4,378 f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level)f'lar User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 11 h 4 m 57 sec
Reputation Power: 1509
Send a message via Google Talk to f'lar
If only the first 50 or 60 bytes are needed, then for the love of God why are you reading in the entire file?!?

Reply With Quote
  #5  
Old April 30th, 2012, 07:43 PM
NeoScyther NeoScyther is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 12 NeoScyther User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 32 m 26 sec
Reputation Power: 0
Quote:
Originally Posted by f'lar
If only the first 50 or 60 bytes are needed, then for the love of God why are you reading in the entire file?!?


As stated, I at first had a way to read in each character, but I don't think it hit me until I changed the code that I was reading in the file/converting the characters wrong. I was reading the file character by character, converting each character to Hex based on the ASCII value of the character I read in, and that's where the problems began. Anything with an ASCII value higher than 127 gave my program an issue. That's when I dug around on the net and found the line of code in the first post that I used, then actually tried to combine the two ideas, and just got stuck on how to do it.

Bottom line, I'm reading in the entire file because I can't seem to find a good way to only read in the first fifty bytes or so while converting each character to Hex and not giving me issues. I've written several programs for myself in my spare time, but this is my first attempt at reading a file and converting the characters from one type to another.

Reply With Quote
  #6  
Old May 15th, 2012, 02:29 PM
Twyn Twyn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Location: Canada
Posts: 1 Twyn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 47 m 59 sec
Reputation Power: 0
Something like this maybe?

Code:
byte[] buffer = new byte[300];
File.OpenRead(<filename>).Read(buffer, 0, 300);
rtbInp.Text = BitConverter.ToString(buffer);


Adjust the byte array length and count parameter of the .Read as appropriate

If you don't want the dashes then add .Replace("-", string.empty) to the end of the Conversion line.

Anywho,

Cheers!
Twyn

Reply With Quote
  #7  
Old May 15th, 2012, 10:34 PM
NeoScyther NeoScyther is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 12 NeoScyther User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 32 m 26 sec
Reputation Power: 0
Surprisingly, I figured it out. I did some searching online and found the following:

Code:
Using myByteReader As New IO.BinaryReader(IO.File.Open("insertfilename", IO.FileMode.Open))
  ByteArr = myByteReader.ReadBytes(64)
End Using


That actually worked perfectly. I do thank everyone for the suggestions though.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - More.Net Development > [VB.NET] Getting an Out Of Memory exception when opening large files.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap