Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects during runtime

 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This code prints out the available locales using a Numberformat object and formats a double using these locales in an extended for-loop. Per my understanding, this code creates multiple numberformat objects but 'nf2' will only refer/point to one at a time as the loop progresses. The objects that do not have a reference any longer will be garbage collected the next time gc runs. Right/wrong?




This code takes between 7-8 secs to run. Is there anyway of making it more efficient? Thanks.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given that getCurrencyInstance is static, you're probably not creating any objects at all. Rather, nf2 is just redefined to point at a different object each time. Yes, unreferenced objects will (eventually) get gc'd.

John.
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote: Rather, nf2 is just redefined to point at a different object each time.


And how and when are these objects created?

Thanks.
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:Given that getCurrencyInstance is static, you're probably not creating any objects at all.

Actually, since NumberFormat is mutable, the getXXXInstance methods all return a new object each time. It's just the pattern etc that will be set automatically.

Gina vernon wrote:And how and when are these objects created?

Inside the static methods for NumberFormat. For other classes with factory methods, the objects may be created when the class is loaded. Sometimes there is a mix between these two; Integer.valueOf is an example. For values between -128 and 127 it returns cached objects that are created when the class is loaded, for all other values it will create a new object.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob:

Thanks for the clarification. I guess I'll have to spend some time going through the source .

John.
 
Gina vernon
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.
 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The garbage collector will take care of any 'lost' (unreachable) objects, but at an unknown time.

That code completed in about a sec and I have a 900MHZ processor. I had a online flash player running, two tabs in mozilla open, eclipse IDE running, the windows explorer open, a pdf file open, and windows media player open. hahah!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic