Author: markt
Date: Wed Jun  3 13:17:54 2015
New Revision: 1683321

URL: http://svn.apache.org/r1683321
Log:
Increase default buffer size to match minimum frame size.
Use lazy init for buffers to reduce memory footprint

Modified:
    tomcat/trunk/java/org/apache/coyote/http2/Stream.java

Modified: tomcat/trunk/java/org/apache/coyote/http2/Stream.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http2/Stream.java?rev=1683321&r1=1683320&r2=1683321&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http2/Stream.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http2/Stream.java Wed Jun  3 13:17:54 
2015
@@ -358,19 +358,24 @@ public class Stream extends AbstractStre
          * corruption by careful use of the buffer it would still require the
          * same copies as using two buffers and the behaviour would be less
          * clear.
+         *
+         * The buffers are created lazily because 32K per stream quickly adds
+         * up to a lot of memory and most requests do not have bodies.
          */
         // This buffer is used to populate the ByteChunk passed in to the read
         // method
-        private final byte[] outBuffer = new byte[8 * 1024];
+        private byte[] outBuffer;
         // This buffer is the destination for incoming data. It is normally is
         // 'write mode'.
-        private final ByteBuffer inBuffer = ByteBuffer.allocate(8 * 1024);
+        private ByteBuffer inBuffer;
 
         private boolean endOfStream = false;
 
         @Override
         public int doRead(ByteChunk chunk) throws IOException {
 
+            ensureBuffersExist();
+
             int written = 0;
 
             // Ensure that only one thread accesses inBuffer at a time
@@ -405,7 +410,21 @@ public class Stream extends AbstractStre
 
 
         public ByteBuffer getInBuffer() {
+            ensureBuffersExist();
             return inBuffer;
         }
+
+
+        private void ensureBuffersExist() {
+            if (inBuffer != null) {
+                return;
+            }
+            synchronized (this) {
+                if (inBuffer == null) {
+                    inBuffer = ByteBuffer.allocate(16 * 1024);
+                    outBuffer = new byte[16 * 1024];
+                }
+            }
+        }
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to