Help coderanch get a
new server
by contributing to the fundraiser
  • 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

q. on garbage collection

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin, Atul, thank you for your explanation!
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic