• 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

Debate - Declarations in/outside loops

 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?)
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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:

 
Is that a spider in your hair? Here, threaten it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic