First you should try to use StringBuilder, which is the
non-synchronized version of StringBuffer.

Next you could just write:

String price = String.format("$ %.2f",pricef);

which saves you a temporary StringBuffer allocation.

R/


On Mon, Apr 13, 2009 at 7:37 PM, DavidG <[email protected]> wrote:
>
> In my ListView, there's one line of code in my ViewBinder that causes
> lots of garbage collection 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 the line with String.format, the garbage collection
> 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