我在我的应用程序中遇到了内存泄漏问题,触发了多次GC并导致性能问题。我使用MAT生成了一个“泄漏嫌疑报告”。以下是报告内容:
问题嫌疑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()方法。以下是代码:
问题嫌疑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);
}
}
});
}
}
}
请帮我纠正我的代码。谢谢!
注意:自从Honeycomb之后,GC会收集位图,因此不必强制回收它!因此,我没有使用bitmap.recycle()
。