One line causing lots of garbage collection. String.format with float. Help?

by Marco Nelissen » Tue, 14 Apr 2009 11:40:38 GMT

Sponsored Links
 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.


One line causing lots of garbage collection. String.format with float. Help?

by Raphael » Tue, 14 Apr 2009 13:04:32 GMT

 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.



Sponsored Links

Other Threads

1. How to change icon in Alert Dialog box?


I want to change the default icon that appears on Alert Dialog box.
Here is my code:

 public void onClick(View view) {
                Drawable d = Drawable.createFromPath("/data/local/tmp/images");
                        new AlertDialog.Builder(this)
                .setMessage("Some Message")
                .setNeutralButton("Close", new
DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dlg, int sumthin) {


I have pushed .png image.

No errors but i don;t see the change.



2. Mock Location Provider [SOLVED+IMPROVED]

I somehow improved Justins code.


It has now a New-York based journey (longer than before) and the
time-interval between each points can be set so that the update inveral
(set to 500ms) can be arbitraryly small (see line 177)

@Justin: if you like it, please include it in you zip-file


3. No Bluetooth API in 0.9 or 1.0 SDK?!?!?

4. NumberPicker not accessible

5. ListPreference with int values

6. ListPreferency with int values

7. MediaRecord Audio - working example? / raw data stream onPictureTaken()? / Bug in Pictures application?