毕加索的内存不足错误 [英] Out of memory Error in picasso
问题描述
我正在使用picasso库从服务器在网格上加载多个图像. 我在主要活动上有三个片段,每个片段都使用毕加索从服务器上的网格中加载多个图像.当我连续从一个片段导航到另一个片段时,片段加载缓慢,然后半个小时后,由于毕加索的内存不足错误,应用崩溃.如何解决?
I am using picasso library for loading multiple images on grid from server. I have three fragments on main activity.Each fragment loads multiple images on grid from server using picasso. when i navigate from fragment to fragment continously ,the fragments loads slow and then after half hour app get crash due to out of memory error in picasso. How to resolve it?
public class MyAlbImageAdapter extends BaseAdapter {
public List<MyAlbum> _albumList=AppController.getInstance().getPrefManger().getMyAlbums();
public static int flag=0;
private LayoutInflater inflater;
private DisplayImageOptions options;
ImageLoaderConfiguration config;
ImageLoader imageLoader;
Context c;
public MyAlbImageAdapter(Context context,List<MyAlbum> album)
{
_albumList=album;
inflater = LayoutInflater.from(context);
this.c=context;
}
@Override
public int getCount() {
return _albumList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if(v == null)
{
v = inflater.inflate(R.layout.myalb_outer_griditem, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) v.findViewById(R.id.thumbnail);
holder.t1 = (TextView) v.findViewById(R.id.alb_name);
holder.t2 = (TextView) v.findViewById(R.id.usr_name);
v.setTag(holder);
}
else
{
holder = (ViewHolder) v.getTag();
}
MyAlbum p = _albumList.get(position);
holder.t1.setText(p.getName());
Picasso.with(c).load(AppConst.BASE_IMAGE_URL+p.getCover()).fit().centerCrop().into(holder.imageView);
return v;}}
请帮助,我尝试了许多网上链接,但没有发现任何有用的信息.
Please help, i tried many links on net but didn't find anything useful.
错误是:
07-10 12:40:46.230: E/dalvikvm(17680): Out of memory: Heap Size=131107KB, Allocated=129839KB, Limit=65536KB
07-10 12:40:46.240: E/dalvikvm(17680): Extra info: Footprint=131043KB, Allowed Footprint=131107KB, Trimmed=1452KB
07-10 12:40:46.240: E/Bitmap_JNI(17680): Create Bitmap Failed.
07-10 12:40:46.240: E/Bitmap_JNI(17680): Failed to create SkBitmap!
07-10 12:40:48.012: E/dalvikvm-heap(17680): Out of memory on a 198896-byte allocation.
07-10 12:40:46.230: E/dalvikvm(17680): Out of memory: Heap Size=131107KB, Allocated=129839KB, Limit=65536KB
07-10 12:40:46.240: E/dalvikvm(17680): Extra info: Footprint=131043KB, Allowed Footprint=131107KB, Trimmed=1452KB
07-10 12:40:46.240: E/Bitmap_JNI(17680): Create Bitmap Failed.
07-10 12:40:46.240: E/Bitmap_JNI(17680): Failed to create SkBitmap!
07-10 12:40:48.012: E/dalvikvm-heap(17680): Out of memory on a 198896-byte allocation.
推荐答案
相似的问题. Android Picasso ImageView-内存不足异常MemoryLeak
Android解包"您的图片(即将其解码为位图)时,每个像素将使用4个字节.计算像素的数量,将其乘以4,然后乘以20(图像的数量),您可能会接近100mb的数字.例如,如果您的图像分辨率为1,000,000像素,则为1,000,000 x 4 x 20 = 80mb.
When Android "unwraps" your image (i.e. decodes it to bitmap), it will use 4 bytes per pixel. Count the number of pixels, multiply that by 4 and then by 20 (number of your images) and you'll probably get close to the 100mb figure. For instance, if your images have 1,000,000 pixel resolution, it would be 1,000,000 x 4 x 20 = 80mb.
使用某种LRU缓存或类似的缓存(或使用可为您处理缓存的Universal Image Loader库),仅在需要时加载位图.
Use some kind of LRU cache or similar (or alternatively use Universal Image Loader library which handles caching for you) and only load your bitmaps when you need them.
这篇关于毕加索的内存不足错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!