• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Where should I declare the iterator-variable?

 
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is better from the viewpoint of garbage collection?

a) Declare the iterator-variable within the loop:



b) Declare the iterate-variable before the loop:



I'm not sure whether these examples are equivalent or not.
Does example b) perform better than example a) ?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Oliver,

From the point of GC it almost doesn't make any difference, since in the first case the object pointed to by count will be eligible for GC after each iteration, whereas in the second case, it will be eligible for GC in the next iteration, when count is assigned to a new object.

The only difference is that with the second option there will be a remaining ArticleCount object not eligible for GC, which is the object last assigned to the count variable. You can also access this object outside the loop using the second option.

I think asides from that, the second option is probably slightly more efficient, since you are not having to declare a new variable with each iteration. But I might be wrong on this, and how this is handled probably depends on compiler optimizations as well.
 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oliver Rensen wrote:What is better from the viewpoint of garbage collection?

a) Declare the iterator-variable within the loop:



b) Declare the iterate-variable before the loop:



I'm not sure whether these examples are equivalent or not.
Does example b) perform better than example a) ?




I think this question is quite specific; since you said "from the viewpoint of garbage collection". I think a) is better because the count object is eligible for garbage collection once you leave the loop; just my take.
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In example a), there is created a new local count-variable on the stack for each iteration.
The GC must delete the count-variable from the stack after each iteration, and the GC must delete the ArticleCount-reference from the heap after each iteration.

In example b), there is created only one local count-variable on the stack, pointing always to a new ArticleCount-reference while iterating.
The GC must still delete the ArticleCount-reference from the heap after each iteration, but the GC must delete the count-variable from the stack only once: after the method completes.

This is my understanding, but it can be wrong.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that it almost never makes sense to spend too much time on micro-optimizations like this. The Java compiler and garbage collector are pretty smart and complicated pieces of software which apply a lot of smart optimizations, often more complicated than you've ever imagined. It's very hard to say which construction is better without knowing exactly how the compiler and garbage collector work.

If you really want to find out which is "better", you should write a test program, run it and measure the performance. I'd bet that with the code above, the difference between the two approaches is immeasureably small, so it really doesn't matter what approach you take.
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jesper for your clarification.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic