I’ve been experiencing occasional lags in my Android game when the garbage collector runs. I ran DDMS and discovered that all of the memory being allocated by my application is from this line:
scoreString = String.valueOf(score);
What is the best way to convert an integer to a string without allocating any memory?
Allocate an array of characters to be displayed as the score, and use a lookup table of 0-9 (this conveniently maps to a 0-base array anyway) to append to this array based on each digit of the score.
Edit:
To extract the digits from your score:
Also you’ll know what the max length for the score character array should be based on whatever you’re using to store score (i.e. int)
I recommend reverse-filling this array and initializing it to all ‘0’ so your score will display like