Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

q. on garbage collection

 
Toms Liepins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given:

1. public class NewGarb {
2. public static Object getIt() {
3. Object rg = new Integer(3);
4. Object dg[][] = new Object[1][2];
5. dg[0][1] = rg;
6. dg[0][0] = rg;
7. rg = null;
8. return rg;
9. }
10. }

Which statement is true?
A) The NewGarb class will not compile.
B) The getIt() method must not be declared as static.
C) The NewGarb class compiles, but an exception is received because dg is not set to null.
D) The rg object is eligible for garbage collection after a call to the getIt() method has returned.

The correct answer is D. My question: if array dg was returned instead of Object rg, would that make any difference? I opted for answer B, partly because I was confused with MUST/MAY wording of the answer, and partly because i thought that dg still holds a reference to rg...
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Points in your question:
I think question is formatted in wrong way. Becaues, rg is not object. It is a variable of type Object. So, i do not agree variable is garbage collected. If you make reference of variable rg to null, then object created at line no. 3 will be eligible for garbage collection.

MUSTin point.B :- means, if you include static, then program may fail to compile or runtime exception, or similar type of problem and if you remove static, then this probelm should be eleminated. So B is not correct either.
 
Atul Chandran
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If dg is returned and is assigned to a two dimensional array in the method which called getIt() then the object will not be eligible for garbage collection since two references will be reffering to the object.
 
Toms Liepins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin, Atul, thank you for your explanation!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic