OOM の問題に関する何百もの記事を読みました。ほとんどは大きなビットマップに関するものです。256x256
天気オーバーレイ タイルをダウンロードするマッピング アプリケーションを実行しています。ほとんどは完全に透明で、非常に小さいです。呼び出し中に 442 バイト長のビットマップ ストリームでクラッシュが発生しましたBitmapFactory.decodeByteArray(....).
例外は次のように述べています。
java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=9415KB, Allocated=5192KB, Bitmap Size=23671KB)
コードは次のとおりです。
protected Bitmap retrieveImageData() throws IOException {
URL url = new URL(imageUrl);
InputStream in = null;
OutputStream out = null;
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// determine the image size and allocate a buffer
int fileSize = connection.getContentLength();
if (fileSize < 0) {
return null;
}
byte[] imageData = new byte[fileSize];
// download the file
//Log.d(LOG_TAG, "fetching image " + imageUrl + " (" + fileSize + ")");
BufferedInputStream istream = new BufferedInputStream(connection.getInputStream());
int bytesRead = 0;
int offset = 0;
while (bytesRead != -1 && offset < fileSize) {
bytesRead = istream.read(imageData, offset, fileSize - offset);
offset += bytesRead;
}
// clean up
istream.close();
connection.disconnect();
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeByteArray(imageData, 0, bytesRead);
} catch (OutOfMemoryError e) {
Log.e("Map", "Tile Loader (241) Out Of Memory Error " + e.getLocalizedMessage());
System.gc();
}
return bitmap;
}
デバッガーに表示される内容は次のとおりです。
bytesRead = 442
したがって、ビットマップ データは 442 バイトです。23671KB のビットマップを作成しようとしてメモリ不足になるのはなぜですか?