How to get Javamail to clear the message cache?

I am starting a server that uses Javamail. It has a counter with IMAP IDLE, so some new code is executed when new mail arrives. The list of the new message is passed to my listener as a parameter. I have read this information and will do it. Everything is fine, except that my server has a lot of memory leaks! I made a bunch of heaps and found that the [Lcom.sun.mail.imap.IMAPMessage class uses a lot of memory. The IMAPMessage array seems to be referencing the com.sun.mail.imap.MessageCache object.

Does caching continue? I no longer need to access these messages unless it first appears. How to make the kernel clear the cache? How can i stop javamail from leaking?

+3
source share
3 answers

According to the docs, you pas a boolean value indicating that deleted messages should be deleted:

message.setFlag(Flags.Flag.DELETED, true);
folder.close(aBoolean);
store.close();
+1
source

I found this link in the Javamail forum. How do I get Javamail to clear the message cache?

. "", , - . IMAPMessage , MIMEMessage, . , , IMAPMessage. MIMEMessage . , . InternetHeaders.remove(String)

/**
 * Remove all header entries that match the given name
 * @param   name    header name
 */
public void removeHeader(String name) { 
for (int i = 0; i < headers.size(); i++) {
    InternetHeader h = (InternetHeader)headers.get(i);
    if (name.equalsIgnoreCase(h.getName())) {
    h.line = null;
    //headers.remove(i);
    //i--;    // have to look at i again
    }
}
}

, , MessageCache, .

Field headerField = MimeMessage.class.getDeclaredField("headers");
headerField.setAccessible(true);
InternetHeaders headers = (InternetHeaders) headerField.get(imapMessage);
if (headers != null) {
   Enumeration<?> allHeaders = headers.getAllHeaders();
   ArrayList<String> headerNames = new ArrayList<String>();
   while (allHeaders.hasMoreElements()) {
       Header header = (Header) allHeaders.nextElement();
       headerNames.add(header.getName());
   }
   for (String headerName : headerNames) {
       headers.setHeader(headerName, null);
   }
}

, 40 20 . , . , IMAPMessage, .

+1

You can try a different implementation of Java Mail if you don't like Sun. The GNU Classpath has one (at http://www.gnu.org/software/classpathx/javamail/javamail.html )

0
source

Source: https://habr.com/ru/post/1718383/


All Articles