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.
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
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.
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.