diff options
| author | Martin Ritchie <ritchiem@apache.org> | 2007-07-17 11:38:10 +0000 |
|---|---|---|
| committer | Martin Ritchie <ritchiem@apache.org> | 2007-07-17 11:38:10 +0000 |
| commit | 0579b1408d8dae4d85089fba5f1c8c14e04d1b61 (patch) | |
| tree | c9ed9c9079aa881f03a750287989907ddb264363 /java/common | |
| parent | b101bd671ef4372c91642c8284820595a4c04521 (diff) | |
| download | qpid-python-0579b1408d8dae4d85089fba5f1c8c14e04d1b61.tar.gz | |
QPID-541 A large portion of memory was being wasted in 32k ByteBuffers being held by the AMQShortStrings.
Patch submitted by Robert Godfrey to intern() the AMQSSs to reduce memory usage. Current implementation *will* impact performance due to the usage of a static Map for storage. However, a thread local implementation is in the works.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/branches/M2@556890 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common')
| -rw-r--r-- | java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java index 05e9473463..ec29d62847 100644 --- a/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java +++ b/java/common/src/main/java/org/apache/qpid/framing/AMQShortString.java @@ -26,6 +26,10 @@ import org.apache.mina.common.ByteBuffer; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.lang.ref.WeakReference;
+
/**
* A short string is a representation of an AMQ Short String
* Short strings differ from the Java String class by being limited to on ASCII characters (0-127)
@@ -34,6 +38,10 @@ import org.slf4j.LoggerFactory; */
public final class AMQShortString implements CharSequence, Comparable<AMQShortString>
{
+
+ private static final Map<AMQShortString, WeakReference<AMQShortString>> internMap =
+ new WeakHashMap<AMQShortString, WeakReference<AMQShortString>>();
+
private static final Logger _logger = LoggerFactory.getLogger(AMQShortString.class);
private final ByteBuffer _data;
@@ -43,7 +51,6 @@ public final class AMQShortString implements CharSequence, Comparable<AMQShortSt public AMQShortString(byte[] data)
{
-
_data = ByteBuffer.wrap(data);
_length = data.length;
}
@@ -376,4 +383,27 @@ public final class AMQShortString implements CharSequence, Comparable<AMQShortSt return (length() == name.length()) ? 0 : -1;
}
}
+
+
+ public AMQShortString intern()
+ {
+ hashCode();
+ synchronized(internMap)
+ {
+
+ WeakReference<AMQShortString> ref = internMap.get(this);
+ if(ref != null)
+ {
+ AMQShortString internString = ref.get();
+ if(internString != null)
+ {
+ return internString;
+ }
+ }
+
+ AMQShortString internString = new AMQShortString(getBytes());
+ internMap.put(internString, new WeakReference(internString));
+ return internString;
+ }
+ }
}
|
