File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Debate - Declarations in/outside loops Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Debate - Declarations in/outside loops" Watch "Debate - Declarations in/outside loops" New topic

Debate - Declarations in/outside loops

Alana Sparx
Ranch Hand

Joined: Feb 14, 2006
Posts: 121
Hello all

Bit of a heated debate going on here.

We have an enterprise level system potentially being used by a worldwide community of thousands.

There are some issue in the application code, but I would like to know what your ideas on this is

This to me is the correct way to do things, reusing the same memory address each time the loop iterates.

One of our experienced programmers says this is not an issue, and that this is perfectly acceptable:

Examples of this are littered throughout the code base (where the declaration and assignement are contained in the body of the loop). To me, this runs the risk of memory leaks and stressing the garbage collector, but I am willing to be corrected.

Any thoughts out there?

Non abbreviated thanks 30 seconds after last attempt (are you trying to stop people posting?)
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
someBigObject is an object reference not an object, so it makes no difference whether you declare it inside or outside the loop. The only thing the line
(Object) someBigObject = someLargeOrSmallCollection.get(i);
does it make the reference refer to a different object.

The disadvantage of your method is that when the loop finishes, someBigObject will still be referencing the last object in the collection, so that object will not be available for garbage collection. This may or may not be a problem, as the collection will still hold a reference to the object, so it couldn't be GC'ed anyway. But if the collection was cleared out and there were no other references to the objects in it, then having someBigObject declared outside the loop could be the source of a memory leak.

Using the other method, someBigObject goes out of scope as soon as the loop finishes and is no longer considered as a reference to any object.

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Your "experienced programmer" is right.

The garbage collector does not come into this in the way you imagine. You're talking about a local variable that is an object reference, not an object. The same objects will be created and destroyed in both cases. The difference between the cases is that a local variable will be created once per loop, if it's declared inside the loop, or just once, if it's declared outside the loop.

Local variable creation is very fast. In literally 99.999% of cases, no measurable or perceptible performance difference will occur.

A much more important consideration is readability of code. And, for best readability of code, you should declare variables as close as possible to where they are used. In this case, that's inside the loop, not outside.

Another consideration is that, if you put the declaration outside the loop, and do not null-out the variable's value after the loop, you could risk preventing garbage collection of whatever object the variable refers to. In really bad cases, a large amount of data could be needlessly retained. In most cases, this won't happen, but it's a bigger concern than local variable creation, and is another reason to put the declaration inside the loop.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
[Peter]: The difference between the cases is that a local variable will be created once per loop, if it's declared inside the loop, or just once, if it's declared outside the loop.

Usually this is not a difference at all. Local variables are allocated space on the stack at the beginning of a method, and there's no additional work to be done after that to "create" a local variable inside a loop. You just start using it.

As an example, the following two methods compile to exactly the same bytecode:

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Debate - Declarations in/outside loops
It's not a secret anymore!