title: NioBlockingSelector$BlockPoller class consuming a lot of memory
body: <p>In heap dump NioBlockingSelector$BlockPoller class consuming 3.2GB of heap out of 4GB (allocated) while checking thread dump i found that this class is being used by those threads that were downloading a file (one file of size 35MB and others are less than 5MB). There were total five threads in RUNNABLE state that were downloading file when server got down. Some other threads in BLOCKED state due to logger not consuming memory (Classes used by these threads were not in the BiggestObject list of heap dump checked by jprofiler).</p>
  
  <p>These are the top logs of tomcat process while taken time in last column</p>
  
  <pre><code>9221 tomcat    20   0 7017m 1.8g  26m S 227.3  4.2  89:33.85 java   12:10           
   9221 tomcat    20   0 7017m 2.7g  26m S 361.3  6.3  96:30.57 java   12:15           
   9221 tomcat    20   0 7017m 2.7g  26m S 360.3  6.3  96:30.58 java   12:20           
   9221 tomcat    20   0 7209m 4.5g  26m S 100.1 10.3 111:32.74 java   12:25           
   9221 tomcat    20   0 7209m 4.5g  26m S 98.5 10.3 111:32.75 java    12:30           
   9221 tomcat    20   0 7247m 4.6g  26m S 94.9 10.4 116:40.82 java    12:35           
   9221 tomcat    20   0 7247m 4.6g  26m S 93.3 10.4 116:40.82 java    12:40           
   9221 tomcat    20   0 7224m 4.5g  26m S 98.3 10.3 121:50.08 java    12:45
  </code></pre>
  
  <p>One of the threads using NioBlockingSelector$BlockPoller class</p>
  
  <pre><code>"ajp-nio-13009-exec-33" daemon prio=10 tid=0x00007fac44010800 nid=0x12aa runnable [0x00007fac81b65000]
     java.lang.Thread.State: RUNNABLE
      at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.add(NioBlockingSelector.java:232)
      at org.apache.tomcat.util.net.NioBlockingSelector.write(NioBlockingSelector.java:106)
      at org.apache.tomcat.util.net.NioSelectorPool.write(NioSelectorPool.java:155)
      at org.apache.coyote.ajp.AjpNioProcessor.output(AjpNioProcessor.java:292)
      at org.apache.coyote.ajp.AjpNioProcessor$SocketOutputBuffer.doWrite(AjpNioProcessor.java:541)
      at org.apache.coyote.Response.doWrite(Response.java:533)
      at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:368)
      at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:356)
      at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:396)
      at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:385)
      at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:93)
      at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:86)
      at com.appnetix.app.util.DownloadFileServlet.doGet(DownloadFileServlet.java:58)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
      at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:745)
      at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
      at org.apache.jsp.jsp.intranet.library.user.downloadLibraryFile_jsp._jspService(downloadLibraryFile_jsp.java:309)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  </code></pre>
  
  <p>apache logs of downloaded files only of that time when heap starts to increase form 12:10 to 12:45:</p>
  
  <pre><code>28.186 - - [14/Oct/2015:12:10:12 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1943825730 HTTP/1.1" 200 1514903 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  76.182.246.134 - - [14/Oct/2015:12:10:36 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 200 35598879 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  68.108.85.82 - - [14/Oct/2015:12:11:55 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=875082220 HTTP/1.1" 200 340332 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew#/section/?submenu=xyz&amp;folderNo=1383109718&amp;parentFolderNo=-1&amp;folderType=0&amp;display=no&amp;display=no&amp;folderDetail=true&amp;documentType=G$param$1" "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"
  99.157.61.172 - - [14/Oct/2015:12:12:02 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=2045468694 HTTP/1.1" 200 41195 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  68.108.85.82 - - [14/Oct/2015:12:13:18 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1720586886 HTTP/1.1" 200 353212 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew#/section/?submenu=xyz&amp;folderNo=871601944&amp;parentFolderNo=-1&amp;folderType=0&amp;display=no&amp;display=no&amp;folderDetail=true&amp;documentType=G$param$1" "Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:14:02 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 200 3850232 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  104.180.76.228 - - [14/Oct/2015:12:15:15 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1487852819 HTTP/1.1" 200 2953765 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  104.180.76.228 - - [14/Oct/2015:12:15:28 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1487852819 HTTP/1.1" 200 2953765 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  104.180.76.228 - - [14/Oct/2015:12:16:34 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1487852819 HTTP/1.1" 200 2953765 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  107.107.188.183 - - [14/Oct/2015:12:22:52 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 502 2769 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  206.75.151.76 - - [14/Oct/2015:12:25:36 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 502 2769 "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:27:53 -0400] "GET /massageheight/control/images/franconnect-logo.gif HTTP/1.1" 400 3809 "https://springfield2.franconnect.net/massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:27:53 -0400] "GET /massageheight/control/images/styleerrorNew.css HTTP/1.1" 400 3809 "https://springfield2.franconnect.net/massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:28:18 -0400] "GET /massageheight/control/images/404.jpg HTTP/1.1" 400 3809 "https://springfield2.franconnect.net/massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:13:42 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 503 - "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  206.75.151.76 - - [14/Oct/2015:12:16:28 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 503 - "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  107.107.188.183 - - [14/Oct/2015:12:22:37 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 503 - "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  206.75.151.76 - - [14/Oct/2015:12:14:28 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 503 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
  107.107.188.183 - - [14/Oct/2015:12:22:50 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=485054654 HTTP/1.1" 503 - "https://springfield2.franconnect.net/massageheight/control/main" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"
  108.232.75.214 - - [14/Oct/2015:12:47:05 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=null&amp;documentNo=2050688329 HTTP/1.1" 200 1743914 "https://springfield2.franconnect.net/massageheight/control/searchAllDocuments?documents=3&amp;load=false&amp;documents=2&amp;news=1&amp;faq=1&amp;alerts=1&amp;alerts=1&amp;searchAllDocument=massage%20therapist%20new%20hire&amp;corp=1&amp;fran=1&amp;reguser=1&amp;message=1" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"
  184.96.214.9 - - [14/Oct/2015:12:47:01 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1003477764 HTTP/1.1" 200 7543138 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
  184.96.214.9 - - [14/Oct/2015:12:48:33 -0400] "GET /massageheight/control/downloadLibFile?downloadFlag=toExecuteInsertDocDownload&amp;mimeType=&amp;documentNo=1699912750 HTTP/1.1" 200 46748 "https://springfield2.franconnect.net/massageheight/control/libraryUserHomeNew" "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"
  </code></pre>
  
  <p>Code snippet of Servlet that is downloading file :</p>
  
  <pre><code>byte fileBytes[] = (byte[]) request.getAttribute("FILEBYTES");
  try {
      out = response.getOutputStream();
      out.write(fileBytes);
      out.flush();
      }
  </code></pre>
  
  <blockquote>
    <p>My question is if we are not downloading large files then how come
    NioBlockingSelector$BlockPoller class consuming a lot of memory and
    how this OutOfMemoryError occurred that caused tomcat down.</p>
  </blockquote>
  
  <pre><code>jvm option given to this application  -Xmx:4096 , -Xms:1024
  </code></pre>