I have a method in engine i’m using (andengine) :
public final void setText(String pString){...}
my app is updating score every 1s from static int
mScoreText.setText(""+PlayerSystem.mScore);
The problem is that this way every second a new String object is created , and after 1 minute i have 59 String objects to collect by GC and additional AbstractStringBuilders and init ‘s…
I’ve found a partial solution on andengine forums like this :
private static StringBuilder mScoreValue = new StringBuilder("000000");
private static final char[] DIGITS = {'0','1','2','3','4','5','6','7','8','9'};
mScoreValue.setCharAt(0, DIGITS[(PlayerSystem.mScore% 1000000) / 100000]);
mScoreValue.setCharAt(1, DIGITS[(PlayerSystem.mScore% 100000) / 10000]);
mScoreValue.setCharAt(2, DIGITS[(PlayerSystem.mScore% 10000) / 1000]);
mScoreValue.setCharAt(3, DIGITS[(PlayerSystem.mScore% 1000) / 100]);
mScoreValue.setCharAt(4, DIGITS[(PlayerSystem.mScore% 100) / 10]);
mScoreValue.setCharAt(5, DIGITS[(PlayerSystem.mScore% 10)]);
mScoreText.setText(mScoreValue.toString());
but the main problem remains, .toString() is returning new object every call
Is there any way to solve this?
As far as I know, there is no way to get around the fact that
Stringsare immutable and if your method takes a String, a new one will have to be created every time.