out of memory using java mail

随声附和 提交于 2019-12-14 03:12:11

问题


does java mail API uses streaming? where can i get the source code to confirm this. also i am tryign to send the mail using raw and non-raw mode. in raw mode i can pass an input Stream to MimeMessage constructor:[/b]

new MimeMessage(session, doc.getBodyInputStream());

In Non-raw mode, i have to do the following Since there can be any mime type, so i have to use DataHandler and DataSource. Since the DataSource interface contracts says of providing the fresh inputStream everytime we invoke getInputStream(), we need to keep the data in the byte[] which will throw OOM for large size or documents Is there a way to avoid this?

MimeMessage msg = new MimeMessage(session);
byte[] bArr = doc.getBody();
ByteArrayInputStream ins = new ByteArrayInputStream(
    bArr != null && bArr.length > 0 ?  bArr : "".getBytes());
msg.setDataHandler(new DataHandler( new ByteArrayDataSource(ins, mimeType)));

回答1:


Does the problem occur if you just process a single mail or do you get OOM after processing (and caching!) several mails?

Increasing heap size is an option, but if it just increases the time until you get hurt by the next OOM, then you think about alternatives to keeping the raw byte arrays in memory.

And you should use the ByteArrayDataSource(byte[] bArr, String type) constructor, this prevents from duplicating the complete byte array in memory, because the bArr is just stored in the DataSource as it is. The other constructor starts with an 8k byte array and doubles its size each time more space is needed - this wastes a lot of memory and may be the cause of your problem. (source)




回答2:


Regarding the OOM, in general your mails should not be larger than 10MB, which in turn should not be keeping more than 10MB in the byte array.

Is this a theoretical question? Or are you actually seeing this happen?

If you are seeing it happen, then increase your heap size, since the above code looks more or less correct.




回答3:


Is it possible to dump the body to a (temporary) file, let the dumped bArr get out of scope (or set to null) and then use the FileDataSource?



来源:https://stackoverflow.com/questions/1945390/out-of-memory-using-java-mail

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!