Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

garbage collection

 
maggie karve
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test{
public static void main(String[] args) {
Object a = new Object(); // the object original referenced by object reference a
Object b = new Object();
Object c = new Object();
Object d = new Object();

d=c=b=a;
d=null;
}
}

How many objects are eligible for GC in the following code after d = null?

1. 1
2. 2
3. 3
4. 4

here answer is 3..m getting really confused....
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
before d=null.....d only refers to be object referred by a(which now refers to b)...so when d is made null...only one object is eligible for gc..that is the first one you have made....
 
Waclaw Borowiec
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There're 4 objects created. Line "d=c=b=a" assigns all 4 references to the first created object (initially referenced by a), making 3 other object eligible for GC. Last assignment "d = null" doesn't matter.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry made a wrong diagram....all the obbjects refer to the first diagram...so 3 are eligible for gc..right....oooops
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
assignment operator is right associative. so start working from right on this statement:



when b=a executes, bits in a is copied in b, means now both a and b refers to the same object...so object referred by b previously is eligible for GC.
similarly, for c=b, b and c refers to same object. Means now a,b, and c refer to same object.... so now object referred by c previously is eligible for GC.
again, d=c, so a,b,c and d refer to same object, and object referred by d previously is eligible for GC.

no change on object referred by a.
So total 3.

Hope this helps.
 
Charles Chikito
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There're 4 objects created. Line "d=c=b=a" assigns all 4 references to the first created object (initially referenced by a), making 3 other object eligible for GC. Last assignment "d = null" doesn't matter.


Yes. This is how it works. Assignment operator must be evaluated from R->L

_charles
 
Ankit Garg
Sheriff
Posts: 9528
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Curve please Quote Your Sources when you post a question. This is the 4th time you've asked a mock exam question without quoting the source. Quoting the source is not optional, and if you don't quote your source properly, your question might be deleted...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic