aspose file tools*
The moose likes Java in General and the fly likes Object in a loop scope Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Object in a loop scope" Watch "Object in a loop scope" New topic
Author

Object in a loop scope

Rodrigo Vieira
Greenhorn

Joined: Jul 04, 2008
Posts: 19
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);
}
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


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?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
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

Joined: Mar 02, 2005
Posts: 443
    
    1


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
Sheriff

Joined: Oct 13, 2005
Posts: 40059
    
  28
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object in a loop scope