• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object references  RSS feed

 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

In comparing performance, are the following two the same?


I think in the second case, a object reference is created in every iteration of the loop thus increases the work of the gc (references need to be collected too, right?), am I correct?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they are exactly the same.

Local variables live on the stack, and memory on the stack gets reserved when the method is entered. As a consequence, a local variable declaration doesn't cost any time. References also don't get garbage collected, ever - only objects get. The number of objects that get created and need to be collected is the same in both versions, though.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to mention: The compiler is likely to produce exactly the same byte code for both versions (ignoring debugging information).
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Glad to learn something new.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In today's JVMs this does behave exactly the same as far as object creation and collection are concerned. Though the one design has a reference that outlives the loop. Most JVMs today, even on the design where the reference does not outlive the loop, still won't collect that object until the method itself exits.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could argue the block-local could cause code to be generated that moves the stack frame at the start of each loop eating up a precious nanosecond. :roll:

If only there was a bytecode viewer
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rick O'Shay:
...
If only there was a bytecode viewer


Is that some kind of hint?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a bytecode viewer. Ask google!
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is a byte code viewer and it even has an Eclipse slap-on.

http://andrei.gmxhome.de/bytecode/index.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!