使用Java邮件的内存不足 [英] out of memory using java mail

查看:96
本文介绍了使用Java邮件的内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java邮件API是否使用流传输?我在哪里可以获取源代码来确认这一点. 我也尝试使用原始和非原始模式发送邮件. 在原始模式下,我可以将输入Stream传递给MimeMessage构造函数:[/b]

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());

在非原始模式下,我必须执行以下操作 由于可以有任何mime类型,因此我必须使用DataHandlerDataSource.由于DataSource接口约定每次调用getInputStream()时都提供新鲜的inputStream,因此我们需要将数据保留在byte[]中,这将对大尺寸文件或文档抛出OOM吗?有没有办法避免这种情况?

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)));

推荐答案

如果仅处理单个邮件,还是在处理(和缓存!)多封邮件后得到OOM,会发生问题吗?

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

增加堆大小是一种选择,但是如果它只是增加了下一次OOM所致的时间,那么您可以考虑将原始字节数组保留在内存中的替代方法.

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.

并且您应该使用ByteArrayDataSource(byte[] bArr, String type)构造函数,这可以防止在内存中复制完整的字节数组,因为bArr仅按原样存储在DataSource中.另一个构造函数以8k字节数组开始,每次需要更多空间时其大小都会加倍-这会浪费大量内存,并且可能是导致问题的原因. ()

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)

这篇关于使用Java邮件的内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆