發生 java.lang.OutOfMemoryError: Java heap space
從結果可得知,當使用java.net.HttpURLConnection的OutputStream
會先所有的Data暫存到memory,然後再一次性的送出
而這樣的設計方式,也造成當在傳送大檔時,會有memory不足的問題
要解決這個問題,基本上就兩個方法,加大memory或不要一次全部存完再送
幸運的是 java.net.HttpURLConnection 提供設定internal buffering的功能
以下提供兩種方式解決此問題
方法一:(設定setChunkedStreamingMode)
...
import java.net.HttpURLConnection;
......
URL url = new URL("http://192.168.11.11:9981");
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setChunkedStreamingMode(ChunkLen);
......
import java.net.HttpURLConnection;
......
URL url = new URL("http://192.168.11.11:9981");
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setChunkedStreamingMode(ChunkLen);
......
方法二:(加大memoory)
[Run As]->[Run Configurations...]->[Arguments]->[VM arguments]
-Xms512m
-Xmx1024m
Referece:
1. 此網站有更詳盡的解說HttpURLConnection ( http://www.mzone.cc/article/198.html )
2. Why Developers Should Not Write Programs That Call 'sun' Packages
沒有留言:
張貼留言