• 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

Object in a loop scope

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the better choice thinking about performance and less memory use:

A:
List<MyObject> myList = new ArrayList<>();
MyObject myObject = null;
for (*****){
myObject = new****;
myList.add(myObject);
}


B:
List<MyObject> myList = new ArrayList<>();
for (*****){
MyObject myObject = new****;
myList.add(myObject);
}
 
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would think that in the second case "myObject" would be a candidate for GC when the loop exits.
Whereas in the first it would remain 'alive' until after the outer block completes execution.

I would be of the opinion that unless there are really LARGE number of these objects created inside the loop iterations the later versions of Java should be able to optimize the memory/performance effects.

Would anyone like to elaborate, in case I am mistaken?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajkamal Pillai wrote:
I would think that in the second case "myObject" would be a candidate for GC when the loop exits.
Whereas in the first it would remain 'alive' until after the outer block completes execution.

I would be of the opinion that unless there are really LARGE number of these objects created inside the loop iterations the later versions of Java should be able to optimize the memory/performance effects.

Would anyone like to elaborate, in case I am mistaken?



Remember that it is *objects* (and not references) that are eligible for garbage collection. In both cases, the objects created in the loop are still reachable via the collection after the loop, and hence, are not eligible for GC.

Henry
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
another point...worrying about performance is NOT the smart thing to do, unless you have documented and specific requirements.

You will always be served better by writing clean, maintainable, easy-to-understand code rather than guessing about what might save you a few microseconds in execution time.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Write both methods, and use the javap -c Foo instruction to print the bytecode. I suspect you will find there is no significant difference.
 
Rajkamal Pillai
Ranch Hand
Posts: 479
1
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Henry Wong, Fred rosenberger and Campbell Ritchie.

Henry: I had missed that the Objects are still accessible via the Collection.

I have read that micro performance management unless working with specific requirements is almost always an overkill and could lead to bad coding styles and less readable code with no real noticeable improvement in performance. Hence the comment.

Thanks again for your replies!

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic