简体   繁体   中英

ListView memory allocation while scrolling

I am using jazzylistview with a view holder method. I am noticing when I scroll the list view that more and more memory is getting allocated. It isn't a lot but there is an increase in memory as I am scrolling. I have already tried to remove the image from the list view and typeface. I am running on android api 22. Here is the view holder method for the list view:

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

    if (convertView==null) {
        face = Typeface.createFromAsset(context.getAssets(), "font/Roboto-Regular.ttf");
        face1 = Typeface.createFromAsset(context.getAssets(), "font/Roboto-Thin.ttf");
        convertView = View.inflate(context, R.layout.rss_item, null);
        holder = new ViewHolder();
        holder.itemTitle = (TextView) convertView.findViewById(R.id.textView);
        holder.itemTitle1 = (TextView) convertView.findViewById(R.id.textView1);
        holder.image = (ImageView) convertView.findViewById(R.id.icon);
        holder.itemTitle.setTypeface(face);
        holder.itemTitle1.setTypeface(face1);
        convertView.setTag(holder);
    } else {
        holder= (ViewHolder) convertView.getTag();
    }

    EventItem item = itemList.get(position);
    holder.itemTitle.setText(item.getTitle());
    holder.itemTitle1.setText(item.getDate());
    if (item.getIcon() != 1) {
        holder.image.setImageResource(item.getIcon());
    }
    return convertView;
}

static class ViewHolder {
    TextView itemTitle;
    TextView itemTitle1;
    ImageView image;
}

The Typeface.createFromAsset part of your code may be very heavy. I would recommend reading those 2 fonts in the constructor of your adapter and have 2 global variables you use to set the custom TypeFace.

Other than that, a slight memory increase may be normal especially because the views for a new position may need to be inflated and new objects are created. Android can decide how much memory can allocate to your app and when you reach a certain "limit" it will start dumping some of it.

The only real problem you may have is when memory is continuously being allocated but the garbage collector can not start doing it's thing and free up memory. Basically if you see your graph growing and then getting back to a certain level, you're fine.

Also, if the allocated memory overall is still high, you may have problems somewhere else in your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM