From f5f83aba41813d9abfe25c0dbdd159e28d16617a Mon Sep 17 00:00:00 2001 From: mguessan Date: Wed, 2 Feb 2011 21:20:10 +0000 Subject: [PATCH] Fix 3150426 huge memory usage with IMAP git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1613 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 2 + .../davmail/imap/AbstractImapTestCase.java | 82 ++++++++++++++++++ src/test/davmail/imap/TestImap.java | 85 ++++--------------- src/test/davmail/imap/TestImapFullSync.java | 34 ++++++++ 4 files changed, 135 insertions(+), 68 deletions(-) create mode 100644 src/test/davmail/imap/AbstractImapTestCase.java create mode 100644 src/test/davmail/imap/TestImapFullSync.java diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 99ea7704..5b385232 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -1736,7 +1736,9 @@ public abstract class ExchangeSession { messageList.cachedMimeBody = mimeBody; messageList.cachedMimeMessage = mimeMessage; } + // drop curent message body to save memory mimeMessage = null; + mimeBody = null; } /** diff --git a/src/test/davmail/imap/AbstractImapTestCase.java b/src/test/davmail/imap/AbstractImapTestCase.java new file mode 100644 index 00000000..1eef370c --- /dev/null +++ b/src/test/davmail/imap/AbstractImapTestCase.java @@ -0,0 +1,82 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2011 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.imap; + +import davmail.AbstractDavMailTestCase; +import davmail.DavGateway; +import davmail.Settings; + +import java.io.*; +import java.net.Socket; + +/** + * Abstract IMAP test case. + */ +public class AbstractImapTestCase extends AbstractDavMailTestCase { + static Socket clientSocket; + static BufferedWriter socketWriter; + static BufferedReader socketReader; + + static String messageUid; + + protected void write(String line) throws IOException { + socketWriter.write(line); + socketWriter.flush(); + } + + protected void writeLine(String line) throws IOException { + socketWriter.write(line); + socketWriter.newLine(); + socketWriter.flush(); + } + + protected String readLine() throws IOException { + return socketReader.readLine(); + } + + protected String readFullAnswer(String prefix) throws IOException { + String line = socketReader.readLine(); + while (!line.startsWith(prefix)) { + line = socketReader.readLine(); + } + return line; + } + + @Override + public void setUp() throws IOException { + boolean needStart = !loaded; + super.setUp(); + if (needStart) { + // start gateway + DavGateway.start(); + } + if (clientSocket == null) { + clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.imapPort")); + socketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); + socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + + String banner = socketReader.readLine(); + assertNotNull(banner); + + writeLine(". LOGIN " + Settings.getProperty("davmail.username").replaceAll("\\\\", "\\\\\\\\") + ' ' + Settings.getProperty("davmail.password")); + assertEquals(". OK Authenticated", socketReader.readLine()); + } + } + +} diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index c1ecf9be..0b88f529 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -18,71 +18,20 @@ */ package davmail.imap; -import davmail.AbstractDavMailTestCase; -import davmail.DavGateway; import davmail.Settings; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.internet.MimeMessage; -import java.io.*; -import java.net.Socket; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.util.Random; /** * IMAP tests, an instance of DavMail Gateway must be available */ @SuppressWarnings({"JavaDoc", "UseOfSystemOutOrSystemErr"}) -public class TestImap extends AbstractDavMailTestCase { - static Socket clientSocket; - static BufferedWriter socketWriter; - static BufferedReader socketReader; - - static String messageUid; - - protected void write(String line) throws IOException { - socketWriter.write(line); - socketWriter.flush(); - } - - protected void writeLine(String line) throws IOException { - socketWriter.write(line); - socketWriter.newLine(); - socketWriter.flush(); - } - - protected String readLine() throws IOException { - return socketReader.readLine(); - } - - protected String readFullAnswer(String prefix) throws IOException { - String line = socketReader.readLine(); - while (!line.startsWith(prefix)) { - line = socketReader.readLine(); - } - return line; - } - - @Override - public void setUp() throws IOException { - boolean needStart = !loaded; - super.setUp(); - if (needStart) { - // start gateway - DavGateway.start(); - } - if (clientSocket == null) { - clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.imapPort")); - socketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); - socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); - - String banner = socketReader.readLine(); - assertNotNull(banner); - - writeLine(". LOGIN " + Settings.getProperty("davmail.username").replaceAll("\\\\", "\\\\\\\\") + ' ' + Settings.getProperty("davmail.password")); - assertEquals(". OK Authenticated", socketReader.readLine()); - } - } +public class TestImap extends AbstractImapTestCase { public void testListFolders() throws IOException { writeLine(". LSUB \"\" \"*\""); @@ -138,8 +87,8 @@ public class TestImap extends AbstractDavMailTestCase { mimeMessage.addHeader("bcc", Settings.getProperty("davmail.bcc")); Random random = new Random(); StringBuilder randomText = new StringBuilder(); - for (int i=0;i<10*1024*1024;i++) { - randomText.append((char)('a'+random.nextInt(26))); + for (int i = 0; i < 10 * 1024 * 1024; i++) { + randomText.append((char) ('a' + random.nextInt(26))); } mimeMessage.setText(randomText.toString()); mimeMessage.setSubject("Big subject"); @@ -151,13 +100,13 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals("+ send literal data", readLine()); writeLine(new String(content)); assertEquals(". OK APPEND completed", readFullAnswer(".")); - System.out.println("Create time: "+(System.currentTimeMillis()-start)+" ms"); + System.out.println("Create time: " + (System.currentTimeMillis() - start) + " ms"); writeLine(". NOOP"); assertEquals(". OK NOOP completed", readFullAnswer(".")); start = System.currentTimeMillis(); writeLine(". UID FETCH 1:* (RFC822.SIZE BODY.TEXT)"); readFullAnswer("."); - System.out.println("Fetch time: "+(System.currentTimeMillis()-start)+" ms"); + System.out.println("Fetch time: " + (System.currentTimeMillis() - start) + " ms"); } @@ -325,7 +274,7 @@ public class TestImap extends AbstractDavMailTestCase { messageUid = messageLine.substring(uidIndex, messageLine.indexOf(' ', uidIndex)); assertEquals(". OK UID FETCH completed", readFullAnswer(".")); - writeLine(". UID COPY "+messageUid+" Trash"); + writeLine(". UID COPY " + messageUid + " Trash"); assertEquals(". OK copy completed", readFullAnswer(".")); writeLine(". COPY 1 Trash"); @@ -346,15 +295,6 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } - public void testBrokenPipe() throws IOException, InterruptedException { - testSelectInbox(); - writeLine(". UID FETCH 1:* (RFC822.SIZE BODY.TEXT)"); - socketReader.readLine(); - // force close connection - clientSocket.close(); - Thread.sleep(5000); - } - public void testFetchRfc822Header() throws IOException { testSelectInbox(); writeLine(". UID FETCH 1:* (UID RFC822.HEADER RFC822.SIZE FLAGS)"); @@ -366,4 +306,13 @@ public class TestImap extends AbstractDavMailTestCase { writeLine(". UID FETCH 1:* (UID RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (From To Cc Bcc Subject Date Message-ID Priority X-Priority References Newsgroups In-Reply-To Content-Type)])"); assertEquals(". OK UID FETCH completed", readFullAnswer(".")); } + + public void testBrokenPipe() throws IOException, InterruptedException { + testSelectInbox(); + writeLine(". UID FETCH 1:* (RFC822.SIZE BODY.TEXT)"); + socketReader.readLine(); + // force close connection + clientSocket.close(); + Thread.sleep(5000); + } } diff --git a/src/test/davmail/imap/TestImapFullSync.java b/src/test/davmail/imap/TestImapFullSync.java new file mode 100644 index 00000000..da5126cb --- /dev/null +++ b/src/test/davmail/imap/TestImapFullSync.java @@ -0,0 +1,34 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2011 Mickael Guessant + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.imap; + +import java.io.IOException; + +/** + * Test IMAP full sync. + */ +public class TestImapFullSync extends AbstractImapTestCase { + public void testSelectTrash() throws IOException { + writeLine(". SELECT Trash"); + assertEquals(". OK [READ-WRITE] SELECT completed", readFullAnswer(".")); + writeLine(". UID FETCH 1:* (UID RFC822.SIZE FLAGS BODY.PEEK[HEADER.FIELDS (From To Cc Bcc Subject Date Message-ID Priority X-Priority References Newsgroups In-Reply-To Content-Type)])"); + assertEquals(". OK UID FETCH completed", readFullAnswer(".")); + } + +}