Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 _greyfox()   Members   -  Reputation: 1128

Like
0Likes
Like

Posted 16 May 2013 - 07:41 AM

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?


Edited by Edvinas Kilbauskas, 16 May 2013 - 07:42 AM.

“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

Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 5806

Like
5Likes
Like

Posted 16 May 2013 - 07:56 AM

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)

Edited by SimonForsman, 16 May 2013 - 09:45 AM.

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!

#3 _greyfox()   Members   -  Reputation: 1128

Like
0Likes
Like

Posted 16 May 2013 - 08:08 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS