aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects are Garbage collected.Explain Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "How many objects are Garbage collected.Explain" Watch "How many objects are Garbage collected.Explain" New topic
Author

How many objects are Garbage collected.Explain

Siddhu
Ranch Hand

Joined: Oct 02, 1999
Posts: 31
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

Joined: Dec 14, 1998
Posts: 19664
    ∞

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.

permaculture forums
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Dec 14, 1998
Posts: 19664
    ∞

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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 28, 2000
Posts: 477
I think that the answer is 10 since the String tmp reference goes out of scope.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Mar 22, 2000
Posts: 12265
    
    1
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

Java Resources at www.wbrogden.com
Om Sonie
Ranch Hand

Joined: Feb 04, 2001
Posts: 30
But, then what is the final answer? Confused?
Om
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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).]


"JavaRanch, where the deer and the Certified play" - David O'Meara
Om Sonie
Ranch Hand

Joined: Feb 04, 2001
Posts: 30
Thanks Cindy,
Om
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
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

Joined: Jun 17, 2001
Posts: 16
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


------------------
 
I agree. Here's the link: http://zeroturnaround.com/jrebel
 
subject: How many objects are Garbage collected.Explain
 
Similar Threads
GC
garbage collection
interesting gc problem
GC
Variables out of scope eligible for garbage collection