• Post Reply Bookmark Topic Watch Topic
  • New Topic

object creation  RSS feed

 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
what exactly is the difference between

and

in terms of performance, memory.....
shankar
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In both cases you are asking system to create 100 instances of an Object. By using a single variable as a reference, however, the implication is that the system will de-reference each instance upon creating a new one, thereby scoping it for garbage collection.
The only apparent difference here is in what happens when all is said and done. In your second example, the last object created will survive the for loop. In the first example, none of them will survive because the reference lifetime expires with the loop.
[ December 12, 2003: Message edited by: Michael Ernest ]
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so it means that there is no difference in memory usage in the two above examples, right?
when I exit the loop, in the first example, all 100 objects are eligible for GC and in the second example, 99 of them...
I usually code the second way with the idea that "why create variables again and again". but then does creating variables like in the first example really cost memory??
shankar.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

does creating variables like in the first example really cost memory??

No, not a bit. In fact, the two examples are very likely to compile to the same bytecode.
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SV: Ok so it means that there is no difference in memory usage in the two above examples, right?
ME: Well, no meaningful difference is how I would put it. If you want to get all Jim Yingsty about it there is a difference of the size of one Object instance and one object reference. (Added -- and as EFH has pointed out, an optimizing compiler might treat them as synonymous constructs).
SV: I usually code the second way with the idea that "why create variables again and again". but then does creating variables like in the first example really cost memory??
It depends on how you're measuring cost. If all of this is defined within a single method, any memory allocated once the method has run to completion will be reaped from stack space and handed back to the system. If the primary exists as a static element and is therefore going to live for a while, you can calculate the total cost as the reference plus the object itself which now have no reason to go away until the encapsulating instance is tossed. And even then...
[ December 12, 2003: Message edited by: Michael Ernest ]
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys for the clarifications...
shankar
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually use the first style because I think we should set the scope of an object as narrow as needed (in a case when the object is not needed outside the loop).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!