• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

GC

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public void method1()
{
for(int i=0;i<10;i++)
{
String tmp =Integer.toString(i);
System.out.println(tmp);
}
System.out.println("finished"); //10
}
At line how many are available for GC.
Asnwer:8
But i have choose 9. whats wrong in it??
Thanks!

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Some Mock test say that the number of objects available for GC can't be predicted....While some say 10 ......Confused.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems to me that there are 11 objects available for gc at line 10.
There are 10 string objects created in the for loop. Notice that "tmp is declared inside the for loop. At line 10, tmp is out of scope so the 10 string objects have no references to them.
For the 11th, the variable i is also local to the for loop . So when the loop is done it is out of scope and should be available for gc also.
To verfiy these 2 points, just add the following println's between the lines after the for loop and before line 10:
System.out.println(tmp);
System.out.println(k);
The code will not compile because these variables are out of scope.

 
daryl olson
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aru,
I think they might be meaning that you cannot tell if or when the objects get gc'd. But you can tell when an object is available (ie no more refernces to it).
The gc may never run even if you explicitly called via System.gc()as this only suggests that the gc be run.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think avn is right. The answer is 9.
please anyone explain correct answer with details.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, let's forget about garbage collection for a moment. How many objects are there in total? It seems that a number of people here aren't counting the number of loop iterations correctly. When you see "for (int i = 0; i < 10; i++)" you should be able to tell that the loop is executed exactly 10 times. Not 11. Not 9. Exactly 10.
As for the garbage collection part, this is an old question with no definite answer - it turns out it depends on the JVM used. And the reasons are too complex to come up on the exam, so you can safely ignore this question if you just want to do well on the exam. But if you want to learn what's going on here, check out past discussions here and here. (Note that this version of the question has a <= in place of <, so there are 11 objects total here.) Enjoy.
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brogden has the question with a loop like: for(int i=1;i<10;i++), so I answered 9, but the answer was given 8, I don�t know why?
As for the garbage collection part, this is an old question with no definite answer - it turns out it depends on the JVM used.
The question from Bill is: How many "may be" garbage collected?
Bill: correct me if I am wrong please.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill has given further info on this here (which incidentally was linked to in the discussions I linked above). Basically, it turns out that under some Java implementations the last object created doesn't get GC'd until the end of the method, because even though the variable tmp is out of scope after the for loop ends, the reference to the last object may still be on the stack until the method exits, preventing GC of the object. On other implementations, this doesn't happen. It's a strange phenomenon that won't be on the exam.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
How many objects are there in total? Exactly 10.


Hi Jim
THanks for your lucid explanationa and pointers.
I was wondering if in addition to 10 objects coreesponding to variable tmp, will there be one for int i, eligible for GC.
Pls clarify this.
Thanks.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Primitive variables get allocated on the stack.They are pushed and popped at method entry and exit. Therefore, the question of garbage collecting them doesnt arise.
Correct me if I am wrong, Sheriffs and ranch hands
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic