android - asynctaskによるメモリリーク
1649 次
3 に答える
1
問題はdoInBackgroundメソッドのBitmapFactoryにあると思います。このデコードは大量のメモリを消費し、いくつかの既知のリークもあります。画像全体をデコードする代わりに、メモリ消費を減らすために常に画像を拡大縮小します。この例があります:
//decodes image and scales it to reduce memory consumption
private static Bitmap decodeImage(InputStream in, InputStream in2){
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in,null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=100;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
o2s.inTempStorage = new byte[16*1024];
return BitmapFactory.decodeStream(in2, null, o2);
}
注:入力ストリームの2つのインスタンスを開く必要があります。
他のアドバイスは、画像が役に立たなくなるたびにビットマップのメソッドclear()を呼び出すことです。
これがお役に立てば幸いです。
于 2012-10-01T10:24:57.187 に答える
0
以下に示すように、開くたびに入力ストリームを閉じる必要があると思います。
InputStream iStream = null;
try{
URLConnection conn = (new URL(args[0])).openConnection();
iStream = conn.getInputStream();
itmap = BitmapFactory.decodeStream(iStream);
done = true;
}catch(IOException ex){
failedTries++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
} finally {
if(iStream != null)
iStream.close();
}
注:-使用後は、ストリームをすべて閉じることをお勧めします。
于 2012-10-01T10:44:59.927 に答える
0
で作成されたタスクを設定してみてください:
DownloadBitmapTask task = new DownloadBitmapTask(context,image,
holder.cellProgress);
ビットマップのダウンロードが完了するとnullになります。AsyncTaskで割り当てられたすべてのリソースを閉じたり破棄したりしておらず、GCはそれらを解放できません。
于 2012-10-01T10:49:46.900 に答える