Author: trustin
Date: Sun Nov  4 07:16:53 2007
New Revision: 591789

URL: http://svn.apache.org/viewvc?rev=591789&view=rev
Log:
Small memory optimization on CachedBufferAllocator

Modified:
    
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java

Modified: 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java
URL: 
http://svn.apache.org/viewvc/mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java?rev=591789&r1=591788&r2=591789&view=diff
==============================================================================
--- 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java 
(original)
+++ 
mina/trunk/core/src/main/java/org/apache/mina/common/CachedBufferAllocator.java 
Sun Nov  4 07:16:53 2007
@@ -59,7 +59,8 @@
  * @version $Rev$, $Date$
  */
 public class CachedBufferAllocator implements IoBufferAllocator {
-
+    private static final int MAX_POOL_SIZE = 2;
+    
     private final ThreadLocal<Map<Integer, Queue<ByteBuffer>>> 
localRecyclables =
         new ThreadLocal<Map<Integer, Queue<ByteBuffer>>>() {
             @Override
@@ -85,11 +86,16 @@
             buf = ByteBuffer.allocateDirect(capacity);
         } else {
             // Recycle if possible.
-            buf = localRecyclables.get().get(capacity).poll();
+            Queue<ByteBuffer> pool = localRecyclables.get().get(capacity);
+            buf = pool.poll();
             if (buf != null) {
                 buf.clear();
             } else {
                 buf = ByteBuffer.allocate(capacity);
+                // Create one just in case it is used again.
+                if (pool.size() < MAX_POOL_SIZE) {
+                    pool.offer(ByteBuffer.allocate(capacity));
+                }
             }
         }
         return buf;
@@ -149,8 +155,12 @@
             this.buf = newBuf;
             
             // Add to the cache.
-            if (!isDirect()) {
-                localRecyclables.get().get(oldBuf.capacity()).offer(oldBuf);
+            if (!buf.isDirect() && !buf.isReadOnly()) {
+                Queue<ByteBuffer> pool = 
localRecyclables.get().get(buf.capacity());
+                // Restrict the size of the pool to prevent OOM.
+                if (pool.size() < MAX_POOL_SIZE) {
+                    pool.offer(buf);
+                }
             }
         }
 


Reply via email to