AMM
AMM

Reputation: 2205

Android ListView Memory Leak?

I've implemented a sectioned list view so that every section has an adapter using this guide and probably it causes the views to be allocated over and over again when scrolling and not to reuse because there are different adapters (but when scrolling back, it reuses but for the same adapter) and it's getView method is like that (simplified):

        Map< String, ? > object = null;
        String id = null, title = null, rating = null, pic_url = null;
        object = sectionContent.get(position).getMap();
        id = (String) object.get(EventRowValues.ROW_ID);
        title = (String) object.get(EventRowValues.ROW_TITLE);
        pic_url = (String) object.get(EventRowValues.ROW_PIC_URL);

        View hView = convertView;
        if ( hView == null ) {
            hView = mInflater.inflate(R.layout.popularity_row, null);
            ViewHolder holder = new ViewHolder();
            holder.pic = (ImageView) hView.findViewById(R.id.icon);
            holder.title = (TextView) hView.findViewById(R.id.label);
            hView.setTag(holder);
        }

        final ViewHolder holder = (ViewHolder) hView.getTag();

        holder.id = id;

        mImageWorker.loadImage(pic_url, holder.pic);

        holder.title.setText(title);

        return hView;

The loadImage method is from this google example.

The problem is that when I scroll it takes more and more memory, for example 2 MB for 5-10 rows. When I scroll back it doesn't take any more memory but as far as I know it shouldn't allocate such amounts of memory because it reuses the views as I expect it to take memory just when it loads and when I scroll to use the same views and bitmaps from the cache (because much of the drawables are the same object returned from the cache).

What's might be the problem? Any other option to smarter reusable sectioned listView?

Thanks a lot.

Upvotes: 1

Views: 2900

Answers (3)

AMM
AMM

Reputation: 2205

Figured it out, I was using a sectioned table view from the guide I've mentioned before and it won't reuses the cells properly. I've implemented an ExpandableListView with some patches to make it work and now it's great.

Thanks.

Upvotes: 2

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

Try this: UPDATE:

        ViewHolder holder = new ViewHolder();

        if ( convertView == null ) {
            holder = new ViewHolder();
            hView = mInflater.inflate(R.layout.popularity_row, null);
            holder.pic = (ImageView) hView.findViewById(R.id.icon);
            holder.title = (TextView) hView.findViewById(R.id.label);
            hView.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

PS: You have created holder object twice. Is it really needed?

Which android version are you using, coz I also had the same problem, I was using version 11, and on higher version, if you want to be able to scroll list quickly, you need to give android:hardwareAccelerated="true" attribute in activity tag in manifest file.

Upvotes: 2

logcat
logcat

Reputation: 3474

comment out the mImageWorker.loadImage(pic_url, holder.pic); and show difference in memory consumption

Upvotes: 1

Related Questions