1

I am currently working on an android app using LibGDX and have pinpointed my memory leak down to this one method. Can anyone help me figure out where this is coming from? I have tried looking back through native LibGDX methods but am still having trouble finding where the leak is. Thanks

private void drawScore() {
    scoreLength = ("" + myWorld.getScore()).length();
    AssetLoader.shadow.draw(batcher, "" + myWorld.getScore(),
            165 - (3 * scoreLength), midPointY - 82);
    AssetLoader.font.draw(batcher, "" + myWorld.getScore(),
            165 - (3 * scoreLength) - 1, midPointY - 83);
}
Michael
  • 83
  • 9
  • Could be anything...Maybe you dispose the Assets manager at some point, or the Assets manager is static which might cause problems with the Android lifecyle. Maybe you load the fonts not correctly. Could you provide us more code? – Marius Oct 09 '16 at 19:27
  • 2
    Is it leaking a lot? If not it could just be the String concat you do since I guess this drawScore() is done every frame? Try to use StringBuilder instead and see if it gets any better.If it does not, we would need to see AssetLoader.shadow.draw() and AssetLoader.font.draw() – Andreas Toresäter Oct 10 '16 at 05:11
  • How did you identify that there is a leak? Do you simply mean the GC is active here? – Tenfour04 Oct 10 '16 at 13:27
  • It appears Korpen was correct, it was the concat which was creating the effect I described above. I am going to try and redo it using StringBuilder. Thanks! – Michael Oct 12 '16 at 00:06

1 Answers1

0

The best would be to pre-compute all your data so that the render doesn't do any string concatenation or other operations.

So when the score changes:

  1. update the score string and store it in a field;
  2. calculate the position and store it in a Vector2 field.

On drawScore only draw with the data from the fields.

Also you can check GlyphLayout to better compute the position of the string. Apply the same principles as before, do not use new nor computations on draw, but do them when the score changes.

Aleris
  • 7,631
  • 3
  • 32
  • 42