• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many objects are Garbage collected.Explain  RSS feed

 
Siddhu
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a method which creates a number of String objects in the course of printing a
series of messages.
1)public void SoundOff() {
2) for(int i = 0; i < 10; i++) {
3) String tmp = Integer.toString(i);
4) System.out.print(tmp + ",");
5) }
6) System.out.println("10");
7)}
When the program reaches line 6,how many of the String objects created in line 3 are eligible for garbage collection?Assume that
System.out object is not keeping a reference.
a) none
b) 1
c) 8
d) 9
 
paul wheaton
Trailboss
Posts: 22574
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say 9 or 10 is the right answer. I remember that there is some weirdness about a variable inside a block of code going out of scope, but the object it rerferences might not yet be ready for gc. But I don't remember anything for sure about it.
Fortunately, you do not need to know that much detail for the exam.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand why it is not 20 (or 19).
In each iteration, we create one temporary
string when we call toString method and then
one more when we apply the + operator...
Can anyone please explain?
Thanks!
Originally posted by Siddhu:
Here is a method which creates a number of String objects in the course of printing a
series of messages.
1)public void SoundOff() {
2) for(int i = 0; i < 10; i++) {
3) String tmp = Integer.toString(i);
4) System.out.print(tmp + ",");
5) }
6) System.out.println("10");
7)}
When the program reaches line 6,how many of the String objects created in line 3 are eligible for garbage collection?Assume that
System.out object is not keeping a reference.
a) none
b) 1
c) 8
d) 9

 
paul wheaton
Trailboss
Posts: 22574
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The root of the answer lies in "how many of the String objects created in line 3 are eligible for garbage collection?"
Note the "line 3" part.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Wheaton:
I am not shure but this can have to do with the references on the current thread stack. The instances are created on the heap
but the methdoe references on the tread stack so it means that no object is ready untill the methode returns.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say there were 9 objects Eligible for garbage collection.The Objects are eligible for garbage collection only when the refrence counter to that object falls to Zero.So in the Line 3 for every new Object created the refrence variable tmp always points to newly created string Object and hence making all the previously created String Object as Orphan[refrence counter Zero].So only the Lost created Object i.e 10th one has only one refrence pointer tmp.
at line 4 the New String Object is created and is immediatly available for GC.
since the question is interested in How many of from line 3
the answer is 9.
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the answer is 10 since the String tmp reference goes out of scope.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a string is created like this
String x="xyz";
it is not subject to garbage collection, because it is created in the heap and not in the pool. ( Am I right ?)
So, if the line 3 (if i is 10)
String tmp=Integer.toString(i) is equivalent to
String tmp="10";
then the string object is not subject to garbage collection.
Then the answer would be "a) none"
What do you guys think ?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah this is always a tricky question..i have seen some tests say that 9 is correct while what the above says is correct..string literals are not eligible for gc..only when a String obj is created by new() it is....peace
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I ever wrote that question....
It turns out to be quite tricky and depends on the Java version - don't worry, GC questions on the exam are not that weird.
Generally speaking, if an object is un-reachable by any Thread, it can be garbage collected.
Bill
 
Om Sonie
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, then what is the final answer? Confused?
Om
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is either 9 or 10 depending on how the version / venodr of your JVM chose to implement their variable handling mechanism.
The variable i goes out of scope when the for loop is complete. Some implementations may choose to destroy the i variable at that point, making the last object available for the gc.
However, in actuallity, while i goes out of scope and looses visibility to the program after the for loop, MOST vendors implement variable handling on a stack that is related to a "Frame" that corresponds to the method. Therefore the i would continue to exist until the method is over, even if YOU can't get to it. The gc would leave the object alone because SOMETHING still references it.
All that is why Bill was sorry that he put the darn question in the book worded that way. But feel free to harass him about it . I believe that he just wrote ANOTHER book and is going to be doing a promotion and give away on that book shortly.
Yup, Just looked it up and William Brogden will be here NEXT WEEK!
His new book is "Java Developer's Guide to E-Commerce with XML and JSP" by Sybex.
I expect you will find him mostly hanging out in the XML forum.
[This message has been edited by Cindy Glass (edited June 16, 2001).]
 
Om Sonie
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy,
Om
 
Jyotsna Umesh
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Cindy, YOur explanation is always so clear and concrete that I wish I can develop Java understanding someday like you.
Jyotsna
 
junaid rehman
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
eaxactly
u are saying right
catch the right point
junaid
Originally posted by Paul Wheaton:
[B]The root of the answer lies in "how many of the String objects created in line 3 are eligible for garbage collection?"
Note the "line 3" part.
Note all
if any person have good mock on thread pls send me
or also if any person have good examples nearthe real exam on thread pls send me


------------------
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!