• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a large file, getting out of memory error.

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand that I should be expecting this Exception. My question is, is how can I attach a large attachment to an email?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim Yingst
 
Robert Gagliardo
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am writing the attachment to disk first, then using the FileDataSource add the attachment.
 
reply
    Bookmark Topic Watch Topic
  • New Topic