File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Object in a loop scope

 
Rodrigo Vieira
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • 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);
}
 
Rajkamal Pillai
Ranch Hand
Posts: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12022
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • 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: 445
1
Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic