0

After each rotation of GridView memory usage is increasing...

Setting adapter on GridView:

        gridview = (GridView) findViewById(R.id.grid);
        gridview.setAdapter(new MenuAdapter(getApplicationContext(), width, height, density, gridview));
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(getApplicationContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
            }
        });

Adapter getView part:

    public View getView(int position, View convertView, ViewGroup parent) {


    if (convertView == null) {

        gridView = new View(context);
        gridView = inflater.inflate(R.layout.menu_item, null);          

    } else {
        gridView = (View) convertView;
    }


    btn_ = (LinearLayout) gridView.findViewById(R.id.btn);
    textView = (TextView) gridView.findViewById(R.id.menuItem_txt);
    imageView = (ImageButton) gridView.findViewById(R.id.menuItem_img);

    btn_.setBackgroundColor(back_color[position]);      

    textView.setText(menuValues[position]);
    textView.setTextColor(txt_color[position]);
    textView.setTypeface(font);

    imageView.setScaleType(ImageButton.ScaleType.CENTER_CROP);
    imageView.setFocusable(false);
    imageView.setClickable(false);
    imageView.setAdjustViewBounds(true);
    imageView.setColorFilter(img_color[position]);
    imageView.setImageResource(img[position]);


    return gridView;
}

After removing part between "else{}" and "return" statement there are no memory increasing, so i guess problem is somewhere in removed code...?

mgulan
  • 795
  • 1
  • 14
  • 33

1 Answers1

0

Remove this

 gridView = new View(context); // not required

Use a ViewHolder pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

static class ViewHolder
{
TextView tv;
ImageView iv;
LinearLayout b;
}

Change getView to

@Override
public View getView(int position, View convertView, ViewGroup parent) {
 ViewHolder holder;

if (convertView == null) {
    convertView = inflater.inflate(R.layout.menu_item, null);     
    holder = new ViewHolder();
    holder.iv = (ImageButton) convertView.findViewById(R.id.menuItem_img);   
    holder.b = (LinearLayout) convertView.findViewById(R.id.btn);
    holder.tv = (TextView) convertView.findViewById(R.id.menuItem_txt);
    convertView.setTag(holder);
} else {
     holder = (ViewHolder) convertView.getTag();
}


holder.b.setBackgroundColor(back_color[position]);      
holder.tv.setText(menuValues[position]);
holder.tv.setTextColor(txt_color[position]);
holder.tv.setTypeface(font);

holder.iv.setScaleType(ImageButton.ScaleType.CENTER_CROP);
holder.iv.setFocusable(false);
holder.iv.setClickable(false);
holder.iv.setAdjustViewBounds(true);
holder.iv.setColorFilter(img_color[position]);
holder.iv.setImageResource(img[position]);


return convertView;
}
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 1
    @mgulan specify the problem with more details. I am pretty sure using view holder helps – Raghunandan Dec 22 '13 at 17:02
  • @mgulan post your updated code full and specify the exception – Raghunandan Dec 22 '13 at 17:03
  • @mgulan edited my code. but i don't see a problem unless you specify the exception and the updated code. use `convertView` a copy paste error. typo mistake and look at http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works. might help you understand how listview works – Raghunandan Dec 22 '13 at 17:08
  • @mgulan how about scaling the image down. Download BitmapFun.zip. try the code http://developer.android.com/training/displaying-bitmaps/load-bitmap.html. – Raghunandan Dec 22 '13 at 17:13
  • @mgulan by how much is it increasing. Any stats available?. Do you see any performance change now and before using view holder. What is the expected result?. – Raghunandan Dec 22 '13 at 17:15
  • Bitmaps are scaled. It's increasing about 0.5 MB per rotation. No performance change, it's fast... – mgulan Dec 22 '13 at 17:21
  • @mgulan i don't think there is much you can do. May be use lazy loading. try the example in the link i provided in my previous comment. See if there is any difference between your app performance and the code in the link. The only difference is they load images from server and they use caching. The loading of images in fast. – Raghunandan Dec 22 '13 at 17:27
  • @mgulan also comment this `holder.tv.setTypeface(font);` and see if there is any performance change ie memory decreasing – Raghunandan Dec 22 '13 at 17:28
  • 1
    @mgulan http://stackoverflow.com/questions/20599669/bad-performance-of-custom-listview-listview-items-name-gets-converted-from-data/20599826#20599826. see this. Very similar. Plus here i have suggested ViewHolder pattern also. Using ViewHolder pattern does help. But i don't think there is much you can do since you say image is already scaled down. Apart from that `Listview` recycles views. – Raghunandan Dec 22 '13 at 17:32