File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes JavaMail 1.2 API - Out Of Memory Error when number of attachments to be downloaded is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "JavaMail 1.2 API - Out Of Memory Error when number of attachments to be downloaded is" Watch "JavaMail 1.2 API - Out Of Memory Error when number of attachments to be downloaded is" New topic
Author

JavaMail 1.2 API - Out Of Memory Error when number of attachments to be downloaded is

Munish Jauhar
Greenhorn

Joined: Nov 12, 2001
Posts: 1
Hi
We are facing a problem with the JavaMail API. Let me explain the problem to you we are developing a Swing based email application which connects to a pop3 mail server, the applications then downloads the attachments from the mail to a specified machine(the attachments are JPG/TIF image files and CSV files, some of the TIF files are 700k-800k in size). The problem is that our applications runs fine upto around 150 odd mails then it throws an out of memory error. Our mail server currently has around 3600 mails. Intially we saw the code on how to download attachments from http://www.jguru.com/faq/view.jsp?EID=26996 , At first we thought that maybe the error was in our Swing code or in some other part of our code but gradually we narrowed down the code to the following three methods(given below - this is sample code) and more specifically to the method
public static void handlePart(Part part)
If we comment out all calls to this method then the main method executes, perfectly but as soon as we start making calls to this method we are able tp parse 150 odd messages before we get the out of memory error. We commented out all calls to the method saveFile(String filename,InputStream input) and ran the code but still the same problem. We tried running
System.runfinalization()
System.gc()
but no luck the problem persisted
we also used:
System.out.println("Total Memory: " + java.lang.Runtime.getRuntime().totalMemory());
System.out.println("Free Memory: " + java.lang.Runtime.getRuntime().freeMemory());
System.out.println("Used Memory: " + (java.lang.Runtime.getRuntime().totalMemory() - java.lang.Runtime.getRuntime().freeMemory()));
Free memory was always available even when we got the out of memory error
We tried creating our own connection mechanism wherein we automatically disconnected after processing 50 mails and then freed all the resources and then re-connected again, this process worked fine for around 800-900 mails after which we continued to get the out of memory error.
=================================================================package pop3mail;
import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class GetParts {
public static void main (String args[]) throws Exception {
String host = "hostaddress";
String username = "username";
String password = "mypassword";
Session session = Session.getInstance(new Properties(), null);
Store store = session.getStore("pop3");
store.connect(host, username, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());
Object content = message[i].getContent();
if (content instanceof Multipart) {
handleMultipart((Multipart)content);
}
else {
handlePart(message[i]);
}
}
folder.close(false);
store.close();
}
public static void handleMultipart(Multipart multipart) throws MessagingException, IOException {
for (int i=0, n=multipart.getCount(); i<n; i++) {
handlePart(multipart.getBodyPart(i));
}
}
public static void handlePart(Part part) throws MessagingException, IOException {
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (disposition == null) {
System.out.println("Null: " + contentType);
if ((contentType.length() >= 10) && (contentType.toLowerCase().substring(0, 10).equals("text/plain"))) {
part.writeTo(System.out);
}
else {
System.out.println("Other body: " + contentType);
part.writeTo(System.out);
}
}
else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName() + " : " + contentType);
//saveFile(part.getFileName(), part.getInputStream());
}
else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: " + part.getFileName() + " : " + contentType);
//saveFile(part.getFileName(), part.getInputStream());
}
else {
System.out.println("Other: " + disposition);
}
}
public static void saveFile(String filename,InputStream input) throws IOException {
if (filename == null) {
filename = File.createTempFile("xx", ".out").getName();
}
String filenamepath = "n:\\jdk1.3.1\\bin\\temp\\" + filename;
File file = new File(filenamepath);
for (int i=0; file.exists(); i++) {
file = new File(filenamepath+i);
}
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1) {
bos.write(aByte);
}
bos.flush();
bos.close();
bis.close();
}
}
=================================================================
Can you please tell us as to what is that the problem maybe. Or is there another way to download attachments from a pop3 mail server.
Sincere Regards
Munish Jauhar
 
Don't get me started about those stupid light bulbs.
 
subject: JavaMail 1.2 API - Out Of Memory Error when number of attachments to be downloaded is