私の外部サーバにHttpリクエストを送信して、サイズが100MBの.txtファイルをダウンロードする簡単なコードがあります。 40メガバイトのような小さなファイルは機能していますが、大きなものにはいくつかの問題があります。LibGDXネットでAndroid上に100MBのファイルをダウンロードできません
Net.HttpRequest request = new Net.HttpRequest(Net.HttpMethods.GET);
request.setTimeOut(2500);
String assetsUrl = "http://111.111.111.111/100mb.txt";
request.setUrl(assetsUrl);
// Send the request, listen for the response
// Asynchronously
Gdx.net.sendHttpRequest(request, new Net.HttpResponseListener() {
@Override
public void handleHttpResponse (Net.HttpResponse httpResponse) {
InputStream is = httpResponse.getResultAsStream();
OutputStream os = Gdx.files.local("100mb.txt").write(false);
byte[] bytes = new byte[1024];
int count = -1;
try {
while ((count = is.read(bytes, 0, bytes.length)) != -1) {
os.write(bytes, 0, count);
}
} catch (IOException e) {
e.printStackTrace();
}
}
進捗状況を表示するコードがいくつかありますが、ここでは重要ではありません。 問題は、ファイルのサイズが100MBであるということですが、Androidは魔法のようにそれをダウンロードしながら400メガバイト+ RAMを割り当て、エラーが付属しています:
Waiting for a blocking GC Alloc
WaitForGcToComplete blocked for 12.906ms for cause Alloc
Starting a blocking GC Alloc
Starting a blocking GC Alloc
Suspending all threads took: 35.332ms
Alloc partial concurrent mark sweep GC freed 214(21KB) AllocSpace objects, 1(200MB) LOS objects, 6% free, 216MB/232MB, paused 1.076ms total 130.115ms
Suspending all threads took: 205.400ms
Background sticky concurrent mark sweep GC freed 364(10KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 416MB/416MB, paused 10.448ms total 304.325ms
Starting a blocking GC Alloc Starting a blocking GC Alloc
Alloc partial concurrent mark sweep GC freed 113(3KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 416MB/432MB, paused 290us total 17.611ms
Starting a blocking GC Alloc
Alloc sticky concurrent mark sweep GC freed 31(912B) AllocSpace objects, 0(0B) LOS objects, 3% free, 416MB/432MB, paused 268us total 6.474ms
Starting a blocking GC Alloc
Alloc concurrent mark sweep GC freed 43(13KB) AllocSpace objects, 0(0B) LOS objects, 3% free, 415MB/431MB, paused 268us total 15.008ms
Forcing collection of SoftReferences for 300MB allocation
Starting a blocking GC Alloc
Alloc concurrent mark sweep GC freed 42(1256B) AllocSpace objects, 0(0B) LOS objects, 3% free, 415MB/431MB, paused 286us total 12.426ms
Throwing OutOfMemoryError "Failed to allocate a 314572860 byte allocation with 16770608 free bytes and 96MB until OOM"
私はダウンロードプロセスを実行すると、私は私が1ギガバイト割り当てられている私のデバイス上で見ることができます(システムによる)と600メガバイトフリーの間、アプリは20-30メガバイトを使用しています。数秒後、私のアプリはますます多くのメモリを割り当て始め、400MB以上を使用していることがわかります。ログに記録されているように、最大値に達するとクラッシュが発生します。
おそらく私はそれを理解していないかもしれませんが、それはデータのチャンクを格納するために必要な100MBのRAMだけを割り当てるべきではありませんか? 私はほぼ100%が私のアプリに漏れがないことを確信しています - メモリはダウンロードプロセスによって消費されています。