wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Reading a large file, getting out of memory error. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading a large file, getting out of memory error." Watch "Reading a large file, getting out of memory error." New topic
Author

Reading a large file, getting out of memory error.

Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
I am trying to attach a large file (90+ meg) as an email attachment with javamail.
this is the problem method

is there any way to make this more efficient?
Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
I understand that I should be expecting this Exception. My question is, is how can I attach a large attachment to an email?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Well, as you probably know, the problem is that the ByteArrayOutputStream is trying to allocate a single byte array with over 90 megs of data. If you don't have 90 megs free, this is a problem. You can get a bit more efficiency out of this setup if you know the size in advance - by presizing the ByteArrayOutputStream you eliminate the need to resize and copy. Aside from reducing processor load, this eliminates the memory spike you get during the resizing - and also makes sure the byte buffer isn't larger than it needs to be.
However, you'll probably get much better results if you can find a way to create an attachment that doesn't require the whole thing to be in memory at once. Which is probably the point of your question, but I'm not that familiar with JavaMail. I see that the tutorial here doesn't say anything about requiring a byte array - perhaps you could save the whole thing to a local file first (assuming you aren't just reading from a local file in the first place) and then follow the example there. I have no idea if JavaMail will be OK with a 90-meg attachment though. You might also try posting in Other Java APIs, since this seems to have more to do with how to use JavaMail than it does with the IO streams involved. Good luck...


"I'm not back." - Bill Harding, Twister
Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
Thanks Jim Yingst
Robert Gagliardo
Greenhorn

Joined: Nov 15, 2001
Posts: 24
I am writing the attachment to disk first, then using the FileDataSource add the attachment.
 
Consider Paul's rocket mass heater.
 
subject: Reading a large file, getting out of memory error.