So I'm making a game for Android platform which uses Java for programming. I'm near the end of finishing it, but I noticed that it has very strange hiccups (60-70ms) every 3-4 seconds periodically. And I put timers all inside my code, deleting something, and checking what could cause this.
Then I found it, it would look something like this (but not exactly like this, but the principle is exactly the same):
String str = "FPS: " + fps + " score: " + currentScore;
The currentScore variable changes everyframe.
After deleting this line of code, my game stops lagging!
The only thing I could think of is that adding two or more String objects with variables in Java will create new String object, then if my game runs at 80FPS that would be 80 String objects every second, and then the garbage collector comes in and collects all unused strings, while blocking my thread.
So after all this, my question would be is it true that adding variables to String objects in Java will create garbage? If so, is there any way to add them without creating any garbage?