我在我的應用程序中觸發GC多次並導致性能問題的內存泄漏。我使用MAT
生成了leak suspect report
。下面是報告:位圖內存泄漏 - Android
問題可疑1: 「android.graphics.Bitmap」 的 一個實例由加載 「」 佔用4194368(20.13%)字節。內存在由「」加載的「byte []」的一個實例中累積。
問題可疑2: 類 「android.content.res.Resources」,由 「」 裝,佔據3962504(19.02%)字節。內存在由「」加載的「java.lang.Object []」的一個實例中累積。
問題可疑3:由裝 「android.graphics.Bitmap」 的 一個實例 「」 佔用3145792(15.10%)字節。內存在由「」加載的「byte []」的一個實例中累積。
從報告來看,很明顯內存泄漏是由於位圖造成的。我研究了很多,但無法糾正這種泄漏。請幫助我。我正在使用ImageLoader
類來下載和顯示位圖。要使用這個類,我只需調用displayImage()
方法。這裏是代碼:
public class ImageLoader {
private static ImageLoader imageLoader;
private int maxNoOfConnections = 4;
FileCache fileCache;
ExecutorService executorService;
HttpURLConnection conn;
InputStream is;
OutputStream os;
PhotosLoader photosLoader;
Handler handler;
Bitmap bitmap;
private ImageLoader(Context context) {
fileCache = new FileCache(context);
executorService = Executors.newFixedThreadPool(maxNoOfConnections);
handler = new Handler();
}
public static ImageLoader getInstance(Context context) {
if (imageLoader == null)
imageLoader = new ImageLoader(context);
return imageLoader;
}
public void displayImage(String url, ProgressBar pBar, ImageView imageView) {
photosLoader = new PhotosLoader(url, imageView, pBar);
executorService.submit(photosLoader);
}
private Bitmap getBitmap(String url) {
File f = fileCache.getFile(url);
bitmap = decodeFile(f);
if (bitmap != null)
return bitmap;
try
{
URL imageUrl = new URL(url);
conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
is = conn.getInputStream();
os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex)
{
Log.e("inNews", "Image Url Malformed");
return null;
}
}
private Bitmap decodeFile(File f) {
try
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true)
{
if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e)
{
}
return null;
}
class PhotosLoader implements Runnable {
String url;
ImageView imageView;
ProgressBar pBar;
Bitmap bmp;
public PhotosLoader(String url, ImageView imageView, ProgressBar pBar) {
this.url = url;
this.imageView = imageView;
this.pBar = pBar;
}
@Override
public void run() {
bmp = getBitmap(url);
handler.post(new Runnable() {
@Override
public void run() {
if (bmp != null)
{
pBar.setVisibility(View.GONE);
imageView.setImageBitmap(bmp);
} else
{
pBar.setVisibility(View.GONE);
imageView.setImageResource(R.drawable.img_no_image_grid);
}
}
});
}
}
}
請幫我糾正我的代碼。謝謝!
說明:我沒有使用bitmap.recycle()
,因爲文檔中提到後Honeycomb GC收集位圖並且不再需要強制回收它!
當你旋轉設備時,隨機時間總是在同一時間收到內存泄漏......? – Fustigador 2013-02-18 13:14:42
您應該閱讀有關Android文檔中的SoftReference,在您的情況下,如果您將返回的位圖存儲在某處,則只要執行涉及加載圖像的操作,它就會保留在內存 – 2013-02-18 13:14:48
@Fustigador中。沒有專門旋轉設備。 – gauravsapiens 2013-02-18 13:22:24