• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

gc

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q27.Which of the following regarding garbage collection are true?
Select any two.(from go4java mock exam2)
1) Given,b1 = b2. If b2 is eligible for garbage collection, then b1 is also eligible for garbage collection.
2) An object will be garbage collected immediately after the last reference to the object is removed.
3) Once an object has become eligible for garbage collection, it will remain so until it is destroyed.
4) You cannot force garbage collection.
Ans: 1) and 4)
hi! how can the first option be right???
ashok.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ashok khetan:
Your question is some complex.
b1 = b2 means that b1 change its referance to b2.
So you can think that now b1 and b2 point to the same memory area.
My english is poor .
I don't know whether you can understand me.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------
Given,b1 = b2. If b2 is eligible for garbage collection, then b1 is also eligible for garbage collection.
----------------
b2 becomes eligible for gc when its reference is set to null or it looses all the references it is pointing to.
Incidentally b1 is pointing to b2.
When b2 looses all its references same is the case with b1 which is pointing to b2.
Hence the above statement is correct.
HTH
tvs sundaram
Sun Certified Programmer for Java 2 Platform
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashok,
Imagine you hace the following declarations:
Object o1 = new Object(); // first object created
Object o2 = new Object(); // second object created
o1 = o2;
o2 = null;
First, we create two objects. Then we change the reference of o1 to the one of o2, which means that now o1 and o2 are referencing the second created object. Note that the first created object is now eligible for garbage collection since no more variable are referencing it. Then we assign null to o2, which means that o2 is not anymore referencing the second created object, BUT o1 IS.
So either the question is not very well formulated or I am completely nuts. In the question, b1 and b2 are variables and variables are not garbage collected.
The option 1 should look like this in my understanding:
"Given b1=b2. If the OBJECT referenced by b2 is eligible for garbage collection, then the OBJECT referenced by b1 is also eligible for garbage collection". It's a not big difference but it is very, very important to have well-worded questions.
So I think option 1 is false AS WORDED.
Please I urge you all guys to correct me if I'm wrong.
Thanks
Val
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi tvs
I do not really agree with you, try this code
String s1 = new String("s1");
String s2 = new String("s2");
s1 = s2;
s2 = null;
System.out.println("s1="+s1+", s2="+s2);
The output is:
s1=s2, s2=null
So although s2 is null s1 is still referencing the second string, which makes enormous sense !!
Val
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Val Yes I agree the question could have been expressed better.
I think you are right in your explanation but not in the example because the object two is still reachable after the setting to null, just because the first variable still points to it. Give it a null also, to make the second object unreachable. Regarding the first object given b1=b2 makes it unreachable as long as there isn't any other reference to it, which was not especified in the question. So should we guess it?
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,
that's exactly what I wanted to show but maybe failed to express it correctly. If two variables are referencing the same object and one of the variable is set to null, it does not mean that the second variable does not point to the object anymore, as said by tvs above... Jose I agree with you. Java wouldn't be so popular if such behavior was accepted...
Val
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all:
First of all, the questions appearing in Sun exam will be worded properly - this is what I can say from my experience. That is, if a statement such as b1=b2 or ob1=ob2 appears in a question, then surely there will be a mentioning of what b1, b2, ob1,ob2 etc are.
Next, consider the case
String s1 = new String("s1"); //1
String s2 = new String("s2"); //2
s1 = s2; //3
s2 = null; //4
System.out.println("s1="+s1+", s2="+s2);
Here at //1, a String object with value "s1" is created and the reference variable s1 is made to point to it. Similarly at //2, another String object with value "s2" is created and the reference variable s2 is made to point to it.
Now in //3, s2 is assigned to s1. This means both s1 and s2 now point to the second object created in //2. So the first object created at line //1 has no reference pointing to it and it is now eligible for garbage collection.
At //4, s2 is assigned null. So s2 no longer points to the second object created at //2. But s1 STILL POINTS to this. So after the execution of line //4, no new object is available for garbage collection.
It is important to understand that term "eligiblity for garbage collection" applies only to objects and not to object references or other variables.
So in the following case
ob1=ob2;
ob2=null;
even if it is mentioned that ob1, ob2 are reference variables, actually we do not speak of the eligibility of ob1 and ob2 for garbage collection. Rather we speak of the eligiblity of the objects referred to by these variables ob1 and ob2 for garbage collection.
Now, if the question is as follows:
Consider the following code:
Object ob1=new String("one");
Object ob2=new String("two");
ob1=ob2; //line 1
ob2=null; //line 2
Does the object referenced by ob1 become eligible for garbage collection?

Then the answer is YES. But it is very important note that the object referenced by ob1 becomes eligible for garbage collection, at //line 1 when ob2 is assigned to it and NOT when ob2 is assigned null at //line 2!
Hope this explanation is clear.
Gaja Venkat
Sun Certified Programmer for Java 2 Platform

[This message has been edited by Gaja Venkat (edited September 13, 2001).]
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody tell me why the option 3 is not correct.
3) Once an object has become eligible for garbage collection, it will remain so until it is destroyed.
--Farooq
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because any finalize method could still resurrect an object that is eligable for gc. Resurrecting means to give it a new reference.
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose.
--Farooq
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic