Results 1 to 11 of 11
- 09-10-2012, 12:18 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 15
- Rep Power
- 0
- 09-10-2012, 12:29 AM #2
- 09-10-2012, 09:28 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 15
- Rep Power
- 0
Re: Alternative to DecimalFormat class?
Yep, every time you call a DecimalFormat object, it creates heap allocations which results in garbage collector invocations. Well, it does on Android anyway, which is what I'm developing for.
- 09-10-2012, 11:01 AM #4
- 09-10-2012, 11:05 AM #5
Re: Alternative to DecimalFormat class?
Is the Android garbage collector really so inefficient that creating and discarding objects results in a performance hit? I find that rather surprising.
You could probably roll your own class to parse an int and output the required String, Using a single instance of a StringBuilder internally could mean that the only new object created is the returned String.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-10-2012, 11:50 AM #6
Member
- Join Date
- Jun 2012
- Posts
- 15
- Rep Power
- 0
Re: Alternative to DecimalFormat class?
Calling an existing DecimalFormat object in a loop creates enough garbage to cause a collection every 40 seconds or so. I'm working on a game and this is causing stuttering in my app when the GC kicks in. I also tried String.format but this also creates garbage. I just wanted to double check that there wasn't already a Java class I could use before I spent time on my own solution.
Last edited by wmd; 09-10-2012 at 11:53 AM.
- 09-10-2012, 12:51 PM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Alternative to DecimalFormat class?
How many times are you formatting a number then?
Because DecimalFormat doesn't produce that much data on a single run.Please do not ask for code as refusal often offends.
- 09-10-2012, 01:05 PM #8
Member
- Join Date
- Jun 2012
- Posts
- 15
- Rep Power
- 0
Re: Alternative to DecimalFormat class?
It depends on what the player is doing. It's the player score/high score that I'm formatting.
- 09-10-2012, 05:17 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Alternative to DecimalFormat class?
I suppose if the score racks up rapidly (I'm thinking daka-daka-daka) then possibly.
Just check you are only formatting when you have to (on score change I suppose).Please do not ask for code as refusal often offends.
- 09-10-2012, 05:36 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 15
- Rep Power
- 0
Re: Alternative to DecimalFormat class?
Yeah, I am only updating the scores on score change. Enemies can be destroyed in rapid succession, and there are also other elements which can cause the score to increase quickly. I'm surprised that it seems so elusive as to how to convert an int to a String without creating data on the heap. I've tried all sorts but everything produces heap data. Even reusing a StringBuilder creates heap data as the int has to be converted to a String before it can be appended to a StringBuilder.
- 09-10-2012, 06:04 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Alternative to DecimalFormat class?
If the display needs a String then you have to create something.
That something needs iterations to format it (for each comma).
How else are you going to do that without creating objects (Strings in this case)?
Without really knowing the ins and outs of the problem, though, it's going to be hard to suggest anything.
You could, for example, hold the score as units, thousands and millions, each populating a comma separated field on the screen (a bit like the old pinball machines).
Or look at a way of batching score updates...that might be preferable.Please do not ask for code as refusal often offends.
Similar Threads
-
Dollar sign and DecimalFormat
By javauserjava in forum New To JavaReplies: 7Last Post: 04-05-2011, 11:57 PM -
Advice needed on DecimalFormat class
By israelyan in forum New To JavaReplies: 3Last Post: 01-23-2011, 01:34 AM -
Error involving DecimalFormat
By kamikaze in forum New To JavaReplies: 4Last Post: 10-18-2010, 02:04 AM -
locale and decimalFormat confusion
By bz3x in forum New To JavaReplies: 2Last Post: 05-23-2010, 12:22 AM -
DecimalFormat class
By Java Tip in forum Java TipReplies: 1Last Post: 12-30-2007, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks