• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

garbage collection

 
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....
 
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....
 
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
 
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.
 
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
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • 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...
 
I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic