From dfb8f9e0f2c7e50450bdfc9c49fb7f36f5ec62bc Mon Sep 17 00:00:00 2001 From: Fiouz Date: Sat, 7 Aug 2010 15:10:07 +0000 Subject: [PATCH] Memory management optimisation: help garbage collection by avoiding repeated instantiations of immutable arrays (empty arrays of String, Message, Flag, Address) --- src/com/fsck/k9/mail/Address.java | 4 ++-- src/com/fsck/k9/mail/Message.java | 4 +++- src/com/fsck/k9/mail/internet/MimeHeader.java | 4 +++- src/com/fsck/k9/mail/store/ImapStore.java | 10 +++++++--- src/com/fsck/k9/mail/store/LocalStore.java | 8 +++++--- src/com/fsck/k9/mail/store/WebDavStore.java | 16 ++++++++++------ 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/com/fsck/k9/mail/Address.java b/src/com/fsck/k9/mail/Address.java index 98f4c2a17..3b20f430b 100644 --- a/src/com/fsck/k9/mail/Address.java +++ b/src/com/fsck/k9/mail/Address.java @@ -115,7 +115,7 @@ public class Address if (addressList == null && !"".equals(addressList)) { - return new Address[] {}; + return EMPTY_ADDRESS_ARRAY; } try { @@ -145,7 +145,7 @@ public class Address catch (ParseException pe) { } - return addresses.toArray(new Address[] {}); + return addresses.toArray(EMPTY_ADDRESS_ARRAY); } @Override diff --git a/src/com/fsck/k9/mail/Message.java b/src/com/fsck/k9/mail/Message.java index dd914d926..ed81d1ff8 100644 --- a/src/com/fsck/k9/mail/Message.java +++ b/src/com/fsck/k9/mail/Message.java @@ -8,6 +8,8 @@ import com.fsck.k9.activity.MessageReference; public abstract class Message implements Part, Body { + private static final Flag[] EMPTY_FLAG_ARRAY = new Flag[0]; + private MessageReference mReference = null; public enum RecipientType @@ -156,7 +158,7 @@ public abstract class Message implements Part, Body */ public Flag[] getFlags() { - return mFlags.toArray(new Flag[] {}); + return mFlags.toArray(EMPTY_FLAG_ARRAY); } public void setFlag(Flag flag, boolean set) throws MessagingException diff --git a/src/com/fsck/k9/mail/internet/MimeHeader.java b/src/com/fsck/k9/mail/internet/MimeHeader.java index 9def8279a..99cbb53aa 100644 --- a/src/com/fsck/k9/mail/internet/MimeHeader.java +++ b/src/com/fsck/k9/mail/internet/MimeHeader.java @@ -13,6 +13,8 @@ import java.util.*; public class MimeHeader { + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + /** * Application specific header that contains Store specific information about an attachment. * In IMAP this contains the IMAP BODYSTRUCTURE part id so that the ImapStore can later @@ -93,7 +95,7 @@ public class MimeHeader { return null; } - return values.toArray(new String[] {}); + return values.toArray(EMPTY_STRING_ARRAY); } public void removeHeader(String name) diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index d21a6caf8..b6cde74d6 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -83,6 +83,10 @@ public class ImapStore extends Store private static final String CAPABILITY_COMPRESS_DEFLATE = "COMPRESS=DEFLATE"; private static final String COMMAND_COMPRESS_DEFLATE = "COMPRESS DEFLATE"; + private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; + + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + private String mHost; private int mPort; private String mUsername; @@ -1091,7 +1095,7 @@ public class ImapStore extends Store { throw ioExceptionHandler(mConnection, ioe); } - return messages.toArray(new Message[] {}); + return messages.toArray(EMPTY_MESSAGE_ARRAY); } @@ -1123,7 +1127,7 @@ public class ImapStore extends Store } } } - uids = tempUids.toArray(new String[] {}); + uids = tempUids.toArray(EMPTY_STRING_ARRAY); } for (int i = 0, count = uids.length; i < count; i++) { @@ -1143,7 +1147,7 @@ public class ImapStore extends Store { throw ioExceptionHandler(mConnection, ioe); } - return messages.toArray(new Message[] {}); + return messages.toArray(EMPTY_MESSAGE_ARRAY); } @Override diff --git a/src/com/fsck/k9/mail/store/LocalStore.java b/src/com/fsck/k9/mail/store/LocalStore.java index 17df9ecc6..b2b671972 100644 --- a/src/com/fsck/k9/mail/store/LocalStore.java +++ b/src/com/fsck/k9/mail/store/LocalStore.java @@ -37,6 +37,8 @@ import java.util.regex.Matcher; public class LocalStore extends Store implements Serializable { + private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; + /** * Immutable empty {@link String} array */ @@ -805,7 +807,7 @@ public class LocalStore extends Store implements Serializable } } - return messages.toArray(new Message[] {}); + return messages.toArray(EMPTY_MESSAGE_ARRAY); } @@ -1498,7 +1500,7 @@ public class LocalStore extends Store implements Serializable cursor = mDb.rawQuery( "SELECT message_id, name, value FROM headers " + "WHERE message_id in ( " + questions + ") ", - ids.toArray(new String[] {})); + ids.toArray(EMPTY_STRING_ARRAY)); while (cursor.moveToNext()) @@ -1596,7 +1598,7 @@ public class LocalStore extends Store implements Serializable messages.add(message); } } - return messages.toArray(new Message[] {}); + return messages.toArray(EMPTY_MESSAGE_ARRAY); } @Override diff --git a/src/com/fsck/k9/mail/store/WebDavStore.java b/src/com/fsck/k9/mail/store/WebDavStore.java index 2e074e602..0f644972b 100644 --- a/src/com/fsck/k9/mail/store/WebDavStore.java +++ b/src/com/fsck/k9/mail/store/WebDavStore.java @@ -73,6 +73,10 @@ public class WebDavStore extends Store private static final Flag[] PERMANENT_FLAGS = { Flag.DELETED, Flag.SEEN, Flag.ANSWERED }; + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + + private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; + private int mConnectionSecurity; private String mUsername; /* Stores the username for authentications */ private String alias; @@ -1448,7 +1452,7 @@ public class WebDavStore extends Store } } - return messages.toArray(new Message[] {}); + return messages.toArray(EMPTY_MESSAGE_ARRAY); } @@ -1467,7 +1471,7 @@ public class WebDavStore extends Store if (uids == null || uids.length == 0) { - return messageList.toArray(new Message[] {}); + return messageList.toArray(EMPTY_MESSAGE_ARRAY); } for (int i = 0, count = uids.length; i < count; i++) @@ -1485,7 +1489,7 @@ public class WebDavStore extends Store listener.messageFinished(message, i, count); } } - messages = messageList.toArray(new Message[] {}); + messages = messageList.toArray(EMPTY_MESSAGE_ARRAY); return messages; } @@ -2298,7 +2302,7 @@ public class WebDavStore extends Store public String[] getHeaderList() { - return this.mHeaders.toArray(new String[] {}); + return this.mHeaders.toArray(EMPTY_STRING_ARRAY); } public void setReadStatus(boolean status) @@ -2430,7 +2434,7 @@ public class WebDavStore extends Store hrefs.add(href); } - return hrefs.toArray(new String[] {}); + return hrefs.toArray(EMPTY_STRING_ARRAY); } /** @@ -2445,7 +2449,7 @@ public class WebDavStore extends Store uids.add(uid); } - return uids.toArray(new String[] {}); + return uids.toArray(EMPTY_STRING_ARRAY); } /**