私はJava 6にHttpServerを埋め込みました。これは、クライアントが大きなテキストファイルをダウンロードできるようにハンドルがあります。問題は、サーバーに10以上のクライアントが同時に接続されている場合、メモリ不足例外が発生することです。私は問題がHttpサーバーの周りにあることを前提としています。 )(HttpServer上のjava.lang.OutOfMemoryError大きなデータをダウンロード中
java.lang.OutOfMemoryError: Java heap space
at java.nio.HeapByteBuffer.<init>(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:
GetBytesメソッドに関する提案例外を変更しません:
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();
}
}
今私が得る例外があります。私は毎回それを作成するのではなく、バイト[]への静的な参照を保持しようとしました。そして、私はまだ同じ例外を取得します。
してください、これは一度に2つの問題かもしれないようにあなたのコードの周りにいくつかの「コードの引用符は、」.. – kgautron
@Sophieはこれが聞こえます。 Javaの問題、およびスレッドの処理に関する問題が含まれます。どのように多くの接続がHTTPサーバーに組み込まれていますか?どのようなHTTPサーバーを使用していますか? – bakoyaro
@Sophieまた、ソースをコンパイルするときにdebug = trueを設定すると、スタックトレースに行番号が表示されます。 – bakoyaro