Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.OutOfMemoryError on HttpServer on download large data
    primarykey
    data
    text
    <p>I have java 6 embedded HttpServer. It has a handle which allows clients to download a big text file. The problem is that whenthe server has more then 10 simultaneous clients, i get out of memory exception. I'm prety sure that the problem is around the Http Server.</p> <pre><code> HttpServer m_server = HttpServer.create(new InetSocketAddress(8080), 0); m_server.createContext("/DownloadFile", new DownloadFileHandler() ); public class DownloadFileHandler implements HttpHandler { private static byte[] myFile = new String("....................").getBytes(); //string about 8M @Override public void handle(HttpExchange exchange) throws IOException { exchange.sendResponseHeaders(HTTP_OK, myFile .length); OutputStream responseBody = exchange.getResponseBody(); responseBody.write(myFile ); responseBody.close(); } } </code></pre> <p>Now the exception i get is: </p> <pre><code>java.lang.OutOfMemoryError: Java heap space at java.nio.HeapByteBuffer.&lt;init&gt;(Unknown Source) at java.nio.ByteBuffer.allocate(Unknown Source) at sun.net.httpserver.Request$WriteStream.write(Unknown Source) at sun.net.httpserver.FixedLengthOutputStream.write(Unknown Source) at java.io.FilterOutputStream.write(Unknown Source) at sun.net.httpserver.PlaceholderOutputStream.write(Unknown Source) at com.shunra.javadestination.webservices.DownloadFileHandler.handle(Unknown Source) at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source) at sun.net.httpserver.AuthFilter.doFilter(Unknown Source) at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source) at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(Unknown Source) at com.sun.net.httpserver.Filter$Chain.doFilter(Unknown Source) at sun.net.httpserver.ServerImpl$Exchange.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Exception in thread "pool-1-thread-24" java.lang.OutOfMemoryError: </code></pre> <p>The suggestion regarding the getBytes() doesn't change the exception. i have tried to hold a static reference to byte[] instead of creating it each time. And I still get the same exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload