这个类是获取邮件所有信息的,包括附件信息。获得到50条的时候还可以。超过60条邮件的时候,就会报
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
请问各位朋友,帮我分析怎么回事?谢谢!~ 急!!!
 

public ContentModel getMailInfo(UseConfigModel model, String entityId, String categoryId,Folder folder) throws Exception {
       ContentModel content = new ContentModel();
       try {
               Message msg = null;
               if(folder==null)
                    getFolder(model, "1", "9999/4");
                    msg = folder.getMessage(Integer.parseInt(entityId));
               if (msg != null) {
                   List list = new ArrayList();
                   List<AttachmentModel> attachList = new ArrayList();
                   Address[] address = msg.getFrom();
                   String from = "";
                   for (int i = 0; i < address.length; i++) {
                       from = FrameworkUtil.convertToNull(getDisplayAddress(address[i]));
                   }
                   int start = from.indexOf("<");
                   int end = from.indexOf(">");
                   if (start > -1 && end > 0 && end > start) {
                       from = FrameworkUtil.convertToNull(from.substring(start + 1, end)).trim();
                   }
                   String title = "无主题";
                   Date date = msg.getSentDate();
                   if(date==null)date = new Date();
                   String strMailSubject[] = msg.getHeader("Subject");
                   if (strMailSubject != null) {
                       if (strMailSubject[0] != null && !strMailSubject[0].equals("")) {
                           title = strMailSubject[0];
                           title=new String(title.getBytes("iso8859-1"),"gb2312");//最后加上的改正编码的代码
                           title = MimeUtility.decodeText(title);
                       }
                   }
                   if (msg.isMimeType("text/*")) {
                       list.add(new ContentItemModel("标题:", title));
                       list.add(new ContentItemModel("来自:", from));
                       list.add(new ContentItemModel("发送日期:", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)));
                       list.add(new ContentItemModel("正文:", FrameworkUtil.claerHTML(FrameworkUtil.convertToNull("" + msg.getContent()))));
                       list.add(new ContentItemModel("sender:", from + ";" + FrameworkUtil.convertToNull(msg.getSubject())));
                   } else if (msg.isMimeType("multipart/*")) {
                       list.add(new ContentItemModel("标题:", title));
                       list.add(new ContentItemModel("来自:", from));
                       list.add(new ContentItemModel("发送日期:", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date)));
                       list.add(new ContentItemModel("sender:", from + ";" + FrameworkUtil.convertToNull(null == msg.getSubject() || msg.getSubject().equals("") ? "无主题" : msg.getSubject())));
                       //附件
                       if (msg.getContent() instanceof Multipart) {
                           Multipart mp = (Multipart) msg.getContent();
                           String type = getContentType(mp);
                           boolean addflalg = true;
                           for (int t = 0; t < mp.getCount(); t++) {
                               BodyPart part = mp.getBodyPart(t);
                               String fileName = part.getFileName();
                               String sct = part.getContentType();
                               String disposition = part.getDisposition();
                               if (disposition != null && disposition.equals(Part.ATTACHMENT)) {
                                   fileName = MimeUtility.decodeText(fileName);
                                   AttachmentModel attach = new AttachmentModel();
                                   attach.setName(fileName);
                                   attach.setLink("" + t);
                                   int begin = fileName.lastIndexOf(".");
                                   String ftype = "";
                                   if (begin > -1) {
                                       ftype = fileName.substring(begin, fileName.length()).toLowerCase();
                                       if (ftype.equals(".doc") || ftype.equals(".txt")) {
                                           attach.setType("open");
                                       }
                                   }
                                   attachList.add(attach);
                               } else {
                                   String mcontent = getPart(part, t, 2);
                                   //System.out.println(mcontent+"==");
                                   mcontent = FrameworkUtil.claerHTML(mcontent);
                                  // System.out.println(mcontent+"XXXX");
                                   if (!FrameworkUtil.convertToNull(mcontent).equals("")) {
                                       if (addflalg) {
                                           list.add(new ContentItemModel("正文:", FrameworkUtil.claerHTML(mcontent)));
                                       }
                                       if (type.equals("1")) {
                                           addflalg = false;
                                       }
                                   }
                               }
                           }                     }
                   }
                   content.setContentItems(list);
                   content.setAttachmentList(attachList);
                  }
               } catch (Exception e) {
                   throw e;
               } finally {
                   store.close();
                   folder.close(false);
               }
           return content;
       }
这个类是获取邮件所有信息的,包括附件信息。获得到50条的时候还可以。超过60条邮件的时候,就会报
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
请问各位朋友,帮我分析怎么回事?谢谢!~ 急!!!

解决方案 »

  1.   

     
    dyllove98
    请问你说的临时对象指的什么?能说的更具体些吗?!
      

  2.   

    就是你代码里用过的对象 没用了就XXX=null一下吧
      

  3.   

    ER。。我收个500封以上都没问题啊,而且JVM都是默认配置,估计不是这个类导致溢出,应该是更上层
      

  4.   

    我看过javamail的代码了,是因为javamail是把邮件的数据全读出来放在byte[]里,然后每封邮件的byte[]都放在一个Map里缓存着,所以内存一直不会释放
    只是增加-Xmx也不是解决问题的办法
    我的解决办法是当接收的邮件的字节数达到一定内后,就重新创建Session,重新登录,这样byte[]就会释放了