This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Tricky GC

 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many Object will be Eligible for GC?
public class Numeric
{
public static void main(String[] args)
{
Numeric n = new Numeric();
System.out.println("Hello ");
Object a = new Object();
Object b = new Object();
Object c = new Object();
Object d = new Object();
d=c=b=a;
d=null;
System.out.println();
}
}
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be 3, IMHO.
The assignment is equivalent to d = (c = (b = a)), due to right associativity of the "=" operator.
As such, objects initially referenced by b, c, and d are re-assigned to that initially referenced by a.
Object n still has a reference.
"Hello " literal will not be garbage collected.
I think if you replace the S.o.p.("Hello "); by
System.out.println(new String("Hello ")); then the answer will be 4, since the String object have no reference.
Any opinion?
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also think the answer is 3 objects
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think none of the Object will be garbage collected.
my reason is that even though u set d=null there are still
3 other references pointing to the same object ....
and an object can only be garbage if it is free from all references
pls correct me if i'm wrong
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Kevin. It doesn't matter how many references are pointing to a single Object, or how many references have been set to null. What matters is how many Objects have no reference, which in the above example is 3 (the Objects originally assigned to the b, c, and d reference variables).
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Ranchers,
In the case described NONE of the objects will be garbage collected as setting reference "d" to "null" does not make all the references to the active Object referenced by "a" to "null".
Please note that a object is gc'ed, if and only if, JVM is convinced that there is no way a particular object can be reached. In this case, one can reach the object being refernced to null by other references, viz, a,b,c.
Ravindra Mohan
[This message has been edited by Ravindra Mohan (edited May 29, 2001).]
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravindra, unless I misunderstand what you're saying, I have to disagree

There are no remaining references to Object 2, Object 3, or Object 4. How can you possibly re-obtain handles to those 3 Objects?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Scott. There are 3 objects with no references, a,b and c all point to object1 and d is null.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Think all four objects are eligible. <code>d=c=b=a</code> changes all the references to point to Object 1; Objects 2,3 and 4 are de-referenced. <code>d=null</code> removes the reference to Object 1 so it also becomes eligible for gc.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to Jane and Cindy,
I missed out the point that the objects were in the heap and so setting one of the reference to null, de-references all to null. Thus the answer is all four are eligible for gc.
Thanks again.
Ravindra Mohan.
[This message has been edited by Ravindra Mohan (edited May 29, 2001).]
 
jeena jose
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
object 2,3and 4 are dereferenced when they assigned to object1.but setting d=null won't allow object 1 to gc.since a,b,c are still pointing to it.i think three objects will be ready for gc.am i right??
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with jeena. Can anyone explain this?
Vanitha
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane, I think your statement

d=null removes the reference to Object 1 so it also becomes eligible for gc.

is incorrect.

variables a, b, and c maintain their reference to Object 1 even after d has been set to null. Since Object 1 still has valid references, it should not be eligible for gc.
 
Samith Nambiar
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well 3 it is .. is it


i agree with the ranchers who say 3 ... i jeena has summed it up well


i disagree with the answer 4 (which is including my earlier post ) ... and my reason for that is

i think that even though the reference d is set to null that does not mean that the object is de-referenced of the other 3 references(dont agree with Ravindra at this point)
pls correct me if i'm wrong
Samith.P.Nambiar
<pre>
\```/
(o o) harder u try luckier u get
-------oOO--(_)--OOo----------------------------
</pre>
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think as far as the objects initially referenced by a, b, c and d, all four are available for garbage collection. The reason:
As everyone agrees, three objects are available for gc because all four references point to one object so 3 objects can be gc'd. However, I don't actually think this statement is strictly true. What's happening is:
(d=(c=(b=a)))
a is being assigned the same reference as used by b. (Evaluating from right to left.) Then b and a are assigned the same reference used by c and then finally, c, b and a are assigned the same reference used by d.
Now: a, b, c, and d all have the SAME reference to the same object. Making d=null, implies you make this reference = null.
Therefore: the remaining object which was referenced by the reference in the stack labelled d (and also, c, b and a) is now no longer referenced and is available for gc.
What about that?
 
Vanitha Sugumaran
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When we assign d = null, only d's reference is null, not a,b and c's. I think they still refer the object 1. So I agree with answer 3.
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited May 30, 2001).]
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trevor, I think you are confusing the distinction between reference variables and the Objects which they reference.

Now: a, b, c, and d all have the SAME reference to the same object. Making d=null, implies you make this
reference = null.

The capitalized "SAME" above is incorrect. a,b,c, and d are 4 DIFFERENT references to the same OBJECT. Changing the Object reference of any of the 4 does not change the Object reference of the other 3 (as illustrated by the results of the code I posted above).
Objects -- not references -- are garbage collected.
 
Trevor Green
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, ignore the above. I was so excited about making my first post, I wrote a load of cow dung.
I went wrong somewhere in the middle, saying that a, b and c all now equal the reference assigned to d. It is actually the other way round. If label 'a' refering to the reference in the stack (let's say aX011) which points to Object a in the heap, then this reference value aX011 will be past to b, c and then to d (d=c=b=a). So at this stage, the label d refering to aX011 in the stack points to Object a. When you state d = null; the label 'd' still sits in the stack until the method completes, but its reference is null. Labels 'a', 'b' and 'c' all still hold the value aX011 which still points to Object a so this isn't gc'd.
Sorry for the confusion. I think I've got over my youthful exuberance.
But a question inferred in the original question, but not explicitly answered is: When do Strings (not declared with the new String() method) get cleared out of the String pool?
Any views on that?
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are never garbage collected. I think that's the reason for compiler optimization in order to preserve memory by avoiding duplication.
Some books mention that due to this compiler optimization we should create String using literals rather than the new keyword. I believe this is a fallacious logic. Any ideas?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jane Griscti:
Hi all,
Think all four objects are eligible. <code>d=c=b=a</code> changes all the references to point to Object 1; Objects 2,3 and 4 are de-referenced. <code>d=null</code> removes the reference to Object 1 so it also becomes eligible for gc.

Jane,
I have to disagree with you and agree with Scott.
d=c=b=a means that all of the variables hold the same reference.
d= reference to object 1.
c= reference to object 1.
b= reference to object 1.
a= reference to object 1.
now when we set d=null it does NOT change the values of the other 3 variables. They still hold a reference to object1 which is therefore NOT available for the gc().\
There are only 3 objects available, object2, object3 and object4.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
final word from the posts
only 3 objects are eligible for garbage collection
as i had stated before
makes me happy as this tells me i am preparing well for the exam
maybe (((
 
Sujit Kurtadikar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Sona and Others who says 3 objects will be eligible for GC.
d = c = b = a;
After this, reference count of Object, orignally refered by Object reference a, will be 4. and other 3 will be eligible for GC.
and,d= null; will only reduce the reference count of Only remaining object by 1, and new reference count will be 3.
Sujit
 
Kiran GShetty
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I agree that 3 objects will be garbage collected but then I have to add that I am assuming that the question is :-
How many Object will be Eligible for GC after the line d=null.
But the original question was :-
How many Object will be Eligible for GC ?
In that case I would say 5 objects i.e all the objects created using new keyword (including Numeric). Once the program ends
5 objects will be eligible for GC. The OS will decide when to grabage collect it.
Anyone agreeing with me?
Kiran Shetty
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was acctually about to say the same: All the objects are eligiable for GC, since the program ends, or actually before that: since their references go out of scope at the end of the main-method.
/Mike
 
Kevin Yip
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we accept the tricky version of this question by answering 5, then I've a question:
How do we know whether the System.out object have a reference or not? The answer may be 6, it depends.
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't system static?
//Mike
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yikes ... you are all right ... a, b and c will still point to the original object! Dumb mistake Sorry.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited May 31, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic