title: Large pdf files OutOfMemoryError
body: <p>I have byte array that need to be returned to frontend as a file. For this purpose I use <code>IOUtils.write</code> but if the file is bigger than 15-20MB it throw:
  <em>failed; error='Cannot allocate memory' (errno=12)</em> and my Tomcat 7 dies. </p>
  
  <p>I need to restart it to start working.</p>
  
  <p>Tomcat is starting with 1.5GB RAM and dual core CPU</p>
  
  <p>Here is my code:</p>
  
  <pre><code>OutputStream outputStream = null;
          try{
              outputStream = response.getOutputStream();
              IOUtils.write(imgbytes, outputStream);
              outputStream.flush();
          }catch(IOException ioException){
              logger.error("IO ERROR - ", ioException);
          }finally{
              if(outputStream != null){
                  try {
                      outputStream.close();
                  } catch (IOException e) {
                      logger.error("Error closing stream - ", e);
                  }
              }
          }
  </code></pre>
  
  <p>And here is the error:</p>
  
  <pre><code>[2016-11-21 04:07:02] ERROR [http-bio-8080-exec-7] org.fwo.controller.FileController (FileController.java:43) - IO ERROR - 
  ClientAbortException:  java.net.SocketException: Connection reset
      at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:413)
      at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:371)
      at org.apache.catalina.connector.OutputBuffer.writeBytes(OutputBuffer.java:438)
      at org.apache.catalina.connector.OutputBuffer.write(OutputBuffer.java:426)
      at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:91)
      at org.apache.catalina.connector.CoyoteOutputStream.write(CoyoteOutputStream.java:84)
      at org.apache.commons.io.IOUtils.write(IOUtils.java:1177)
      at org.fwo.controller.FileController.getFile(FileController.java:40)
      at sun.reflect.GeneratedMethodAccessor403.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:212)
      at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
      at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
      at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
      at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
      at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:900)
      at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:827)
      at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
      at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:427)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
      at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.net.SocketException: Connection reset
      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:118)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
      at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
      at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
      at org.apache.tomcat.util.buf.ByteChunk.append(ByteChunk.java:366)
      at org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:240)
      at org.apache.coyote.http11.filters.ChunkedOutputFilter.doWrite(ChunkedOutputFilter.java:119)
      at org.apache.coyote.http11.AbstractOutputBuffer.doWrite(AbstractOutputBuffer.java:192)
      at org.apache.coyote.Response.doWrite(Response.java:520)
      at org.apache.catalina.connector.OutputBuffer.realWriteBytes(OutputBuffer.java:408)
      ... 45 more
  started
  Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000007c1780000, 95420416, 0) failed; error='Cannot allocate memory' (errno=12)
  #
  # There is insufficient memory for the Java Runtime Environment to continue.
  # Native memory allocation (malloc) failed to allocate 95420416 bytes for committing reserved memory.
  # An error report file with more information is saved as:
  # /tmp/hs_err_pid4983.log
  </code></pre>