• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the difference??  RSS feed

 
umar hathab
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know about the objects created inside for loop.I have tested with following two solutions.Both the codes take the same time(apprx)
This may be a simpler,basic questions..but better to know than in confusions..
I want to know what happens when 1000 of objects are created inside loop and assigned to new references..

1)Date d1=new Date();
for(int s=0;s<10000000;s++)
{
Abc obj=new Abc();
}
Date d2=new Date();
System.out.println("Time taken==="+(d2.getTime()-d1.getTime()));

2)Date d3=new Date();
Abc obj;
for(int s=0;s<10000000;s++)
{
obj=new Abc();
}
Date d4=new Date();
//System.out.println("Time taken==="+(d4.getTime()-d3.getTime()));
Thanks,
A.Umar
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In practice, the code in both cases does exactly the same thing. Most compilers will generate effectively the exact same class file from either version of your code. The only real difference between the two is at the compiler level - if you had tried to access obj outside the loop in which it was declared, the compiler would have prevented you from doing so. Since you didn't try to do that, it doesn't matter. Even though the variable obj is declared inside the loop, the compiled class file allocates space for it on the frame at the very beginning of the method, and frees this space at the end of the method.
So, in either case, what happens to the 100 objects? Well, 1000 objects are created. 999 of them are forgotten as soon as the next one is created, and are eligible for garbage collection as soon as it runs. The last one will technically be eligible for GC when the method exits. In practice it can actually get collected before that - HotSpot is capable of detecting that the obj reference is not being used any more, and evidently optimizes its implementation of the class file so as to allow the object to be collected even before the method exits. Strange but true. See Bill Brogden's discussion of this phenomenon, from a sample question that proved far more complex than he intended.
 
Michael Zalewski
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by umar hathab:
1)Date d1=new Date();
for(int s=0;s<10000000;s++)
{
Abc obj=new Abc();
}
Date d2=new Date();
System.out.println("Time taken==="+(d2.getTime()-d1.getTime()));

2)Date d3=new Date();
Abc obj;
for(int s=0;s<10000000;s++)
{
obj=new Abc();
}
Date d4=new Date();
//System.out.println("Time taken==="+(d4.getTime()-d3.getTime()));

These two are probably pretty nearly the same thing. However, the first form is only slightly more efficient in terms of memory used, because once you got outside the loop, you would be able to re-use the memory taken for the reference to obj.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The memory is reused the same way in either case. Compile some code and use javap to look at the class files - you will see that the bytecodes are effectively the same.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!