Managing String Allocations, Garbage Collections in a Game Loop


Last Updated:

  1. AlbertPucciani

    AlbertPucciani Well-Known Member This Topic's Starter

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8
    Hello all,

    I've managed to get my allocations down to next to nothing using DDMS (great tool), this has drastically reduced my GCs to about 1 or 2 every 3 minutes. Still, I'm not happy because those usually cause a noticeable delay in the game (on some phones) when you interact with it.

    Using DDMS, I know what the allocations are, they are Strings being converted from integers used to display game information to the HUD.

    I'm basically doing this:

    int playerScore = 20929;
    String playerScoreText = Integer.toString(playerScore);
    canvas.drawText(playerScoreText, xPos, yPos);
    etc.....

    This happens once each frame update and the HUD system is modular so I plug things in when I need and this can cause 4 or 5 hud elements to allocate Strings and AbstractStringBuilders in DDMS.

    Any way to reduce these further or eliminate all the String allocations and just reuse a String object?
     

    Advertisement
  2. AlbertPucciani

    AlbertPucciani Well-Known Member This Topic's Starter

    Joined:
    Oct 2, 2010
    Messages:
    71
    Likes Received:
    8

Share This Page

Loading...