That's right, with the code above, it still GCs about 13000 every 2
seconds (as long as I'm scrolling the list quickly back and forth). If
I comment out the formatter line (price = mFormatter.format("%.2f",
args).toString();) it doesn't do the GCs anymore, but I of course lose
my formatting.

On Apr 14, 10:28 am, Marco Nelissen <[email protected]> wrote:
> When you say that it hasn't improved yourgarbagecollection problem,
> do you mean that it still GCs 13000 objects every 2 seconds?
>
> On Mon, Apr 13, 2009 at 10:42 PM, DavidG <[email protected]> wrote:
>
> > Thanks for the feedback.. Now I'm trying to use StringBuilder and
> > Formatter, but I just can't get it to stop doing thegarbage
> > collection. I'm not sure how to go about reusing the Formatter. At
> > first, I just tried creating a public static Formatter member variable
> > and using it directly:
>
> > public Formatter mFormatter = new Formatter();
> >  (then later within setViewValue()... )
> > mFormatter.format("%.2f",pricef).toString();
>
> > Unfortunately the formatter seemed to keep "building" onto itself,
> > making each item in the list return a longer and longer string as I
> > scrolled.
>
> > Then I looked into the Android source code and found an example that I
> > tried to mimick. While it gives me the output I expect, it hasn't
> > improved mygarbagecollection problem. I'm just going to post the
> > majority of my ViewBinder class, can someone point out where I'm going
> > wrong?
>
> > <START CODE>
> > import java.util.Formatter;
> > import android.database.Cursor;
> > import android.graphics.Color;
> > import android.graphics.Paint;
> > import android.view.View;
> > import android.widget.ImageView;
> > import android.widget.SimpleCursorAdapter;
> > import android.widget.TextView;
>
> > public class ItemBinder implements SimpleCursorAdapter.ViewBinder {
>
> >        public static final StringBuilder mBuilder = new StringBuilder();
> >        public static final Formatter mFormatter = new Formatter(mBuilder);
> >        public static final Object[] mArgs = new Object[1];
>
> >        public boolean setViewValue(View view, Cursor cursor, int
> > columnIndex) {
> >                switch(view.getId()) {
> >                case R.id.price:
> >                        // Format the price nicely
> >                        String price = cursor.getString(columnIndex);
> >                        final float pricef = Float.parseFloat(price);
> >                        Object[] args = mArgs;
> >                        args[0] = pricef;
> >                        mBuilder.setLength(0);
> >                        price = mFormatter.format("%.2f", args).toString();
> >                        ((TextView)view).setText(price);
> >                        return true;
> >                }
> >                return false;
> >        }
> > }
> > <END CODE>
>
> > On Apr 13, 10:40 pm, Marco Nelissen <[email protected]> wrote:
> >> Don't use String.format(). It creates all kinds of temporary objects
> >> every time you call it.
> >> Instead, create a single StringBuilder and Formatter, and then reuse
> >> those every time you need to format a string.
>
> >> On Mon, Apr 13, 2009 at 7:37 PM, DavidG <[email protected]> wrote:
>
> >> > In my ListView, there'sonelineof code in my ViewBinder that causes
> >> > lots ofgarbagecollection as I scroll through the list, about every
> >> > two seconds...
>
> >> > D/dalvikvm(16312): GC freed 13171 objects / 659576 bytes in 162ms
> >> > D/dalvikvm(16312): GC freed 13122 objects / 654128 bytes in 129ms
> >> > D/dalvikvm(16312): GC freed 13134 objects / 655416 bytes in 142ms
> >> > D/dalvikvm(16312): GC freed 13129 objects / 654840 bytes in 129ms
> >> > D/dalvikvm(16312): GC freed 13149 objects / 655000 bytes in 110ms
> >> > D/dalvikvm(16312): GC freed 13150 objects / 655720 bytes in 127ms
> >> > D/dalvikvm(16312): GC freed 13075 objects / 652256 bytes in 111ms
> >> > D/dalvikvm(16312): GC freed 13232 objects / 659040 bytes in 136ms
> >> > D/dalvikvm(16312): GC freed 13106 objects / 653920 bytes in 110ms
> >> > D/dalvikvm(16312): GC freed 13155 objects / 655152 bytes in 110ms
>
> >> > The offending code is here, which formats a price for each item in the
> >> > list:
>
> >> > String price = cursor.getString(columnIndex);
> >> > final float pricef = Float.parseFloat(price);
> >> > price = new StringBuffer("$").append(String.format("%.
> >> > 2f",pricef)).toString();
> >> > ((TextView)view).setText(price);
>
> >> > If I comment out thelinewith String.format, thegarbagecollection
> >> > goes away. So what's the "right" way to do this to avoid allocations?
> >> > That database field holds an unformatted text string which I'm trying
> >> > to format into proper currency format (example: format "1.5" to
> >> > "$1.50")
>
> >> > Any pointers on how to improve this?
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to