Java 位图和自定义图像视图内存泄漏(Android)
在我的Android应用程序中,我使用自定义ImageView类横幅来显示从Facebook事件中提取的图像。在运行应用程序时,我注意到在运行应用程序时有一个120 MB的大堆。我使用了MAT,我想我已经把它缩小到字节[],最后是ImageView。请检查下面的图片,看看我是如何得出这个结论的 现在,我已经阅读并尝试回收我的位图,使用WeakReferences,创建缓存,但似乎没有任何东西能够减少内存大小。然而,回收我的位图给我带来了一个错误,说我不能使用回收的位图。我觉得这可能是一个提示,因为它将我的位图保留在内存中 该应用程序在我的手机上运行速度并不慢,它甚至只显示CPU使用率为1.5%,即使内存平均为100MB。我将发布我的横幅子类以及如何下载图片,看看是否有人可以帮助我 Banner.java: 因此,作为一个例子,我下载了这样一幅图像:Java 位图和自定义图像视图内存泄漏(Android),java,android,memory-leaks,bitmap,Java,Android,Memory Leaks,Bitmap,在我的Android应用程序中,我使用自定义ImageView类横幅来显示从Facebook事件中提取的图像。在运行应用程序时,我注意到在运行应用程序时有一个120 MB的大堆。我使用了MAT,我想我已经把它缩小到字节[],最后是ImageView。请检查下面的图片,看看我是如何得出这个结论的 现在,我已经阅读并尝试回收我的位图,使用WeakReferences,创建缓存,但似乎没有任何东西能够减少内存大小。然而,回收我的位图给我带来了一个错误,说我不能使用回收的位图。我觉得这可能是一个提示
new BannerLoader(tempEvent.getCover_url(), new WeakReference<ImageView>(thumb_image), context.getResources().getDrawable(R.drawable.placeholder));
谢谢你的帮助 我希望有人对此发表评论——我想看看答案。你有更近的吗?没有。自那以后,我的列表视图变得更大了,我的内存为150MB,内存不足错误明显。我必须解决的一个折衷办法是重写我的自定义适配器getview方法以重用视图并使用viewholder,但这导致我的列表视图出现故障。为了解决这个问题,我删除了列表视图中的两种类型的对象,并将其设置为一个对象,这样可以减少30 mb的内存。所以没有更多的内存,但仍然很慢。我感觉它甚至不是位图,因为当我在登录页面打开android应用程序时,内存是70 mb。有解决方案吗?这类似于
public class BannerLoader{
private String url;
private WeakReference<ImageView> cover;
private LruCache<String, WeakReference<Bitmap>> mMemoryCache;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
public BannerLoader(String cover_url, WeakReference<ImageView> cover, Drawable defaultDrawable) {
this.url = cover_url;
this.cover = cover;
cover.get().setBackground(defaultDrawable);
mMemoryCache = new LruCache<String, WeakReference<Bitmap>>(cacheSize) {
@Override
protected int sizeOf(String key, WeakReference<Bitmap> bitmap) {
// The cache size will be measured in kilobytes rather than
// number of items.
return bitmap.get().getByteCount() / 1024;
}
};
new DownloadImage().execute();
}
public void addBitmapToMemoryCache(String key, WeakReference<Bitmap> bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
public WeakReference<Bitmap> getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
class DownloadImage extends AsyncTask<Void, Void, WeakReference<Bitmap>>{
protected WeakReference<Bitmap> doInBackground(Void... params){
try {
final WeakReference<Bitmap> cachedBitmap = getBitmapFromMemCache(url);
if (cachedBitmap != null){
return cachedBitmap;
} else{
URL pictureURL = new URL(url);
HttpGet httpRequest = null;
httpRequest = new HttpGet(pictureURL.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
InputStream input = b_entity.getContent();
WeakReference<Bitmap> bitmap = new WeakReference<Bitmap>(BitmapFactory.decodeStream(input));
addBitmapToMemoryCache(url, bitmap);
return bitmap;
}
} catch(Exception ex){
ex.printStackTrace();
}
return null;
}
public void onPostExecute(WeakReference<Bitmap> image){
if (image != null && cover.get() != null){
cover.get().setImageBitmap(image.get());
}
}
}
}
new BannerLoader(tempEvent.getCover_url(), new WeakReference<ImageView>(thumb_image), context.getResources().getDrawable(R.drawable.placeholder));