Adding strings in Java creates new objects? (Strange hiccups in android game)

Started by
1 comment, last by Pufixas 10 years, 11 months ago

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?

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh
Advertisement
You should use the StringBuffer(threadsafe) or StringBuilder(not threadsafe, usually faster than StringBuffer) class instead, Strings in Java are immutable and cannot be modified (any operation that modifies strings will create a new string (This is both slow and can trigger garbage collection). alternativly keep the constant parts of the text as separate strings and print them after eachother.

Calling toString on the StringBuilder/Buffer will however create a new String object, so if you can, use the getChars method to copy the builders buffer to a buffer you use to print from. (This only works if your text renderer can render a character buffer and not just strings Edit: also, if you do copy to a buffer, make sure you only allocate that buffer once (if you allocate the buffer each time you copy to it you gain nothing)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

You should use the StringBuffer(threadsafe) or StringBuilder(not threadsafe, usually faster than StringBuffer) class instead, Strings in Java are immutable and cannot be modified (any operation that modifies strings will create a new string (This is both slow and can trigger garbage collection). alternativly keep the constant parts of the text as separate strings and print them after eachother.

Quick, short and to the bullseye answer! Thanks!

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

This topic is closed to new replies.

Advertisement