• 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

GC question from dan mock

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

after new J().m1(); there is no way to access pool of objects created by the J constructor!! why only i1 & i2 ready for GC?? not i3??
Thanks in adv.
 
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you're right Baharathi, this one is trikky, I guess Dan might have tripped.
There is NO way to access the object referenced by the private other variable, since the whole I object that the private other variable belongs to is actually illegible for GC since it has no reference in any active thread.
That's a case of data islands I guess.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bharathi Balu:

after new J().m1(); there is no way to access pool of objects created by the J constructor!! why only i1 & i2 ready for GC?? not i3??
Thanks in adv.


Bharathi,
The constructor does not create any objects. Instead, the objects are created within method m1 which is invoked after the construction process is complete.
The question that goes with the code example is as follows.


Which objects are eligible for garbage collection when method m2 begins to execute?



At that point in time all three references are pointing at the same object.
I think I'll go back and add line numbers that can be used to refer to the objects. Other than that, I think the question is O.K.
Have I missed something?
 
Bharathi Balasubramanian
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan,
Thanks for correcting me! still, I'm not sure why all 3 objects not available for GC.. (even if the reference(s) get reassigned) afer J().m1() finishes the objects are no longer available to J.m2() what so ever!!
am I missing any basics? :roll:
Thanx
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Dan, I guess I missed reading the question, since the header wasn't in the post, I thought the question would be typically "after the line new J().m1(); is executed"
so your answer is correct if it asks "right before executing m2()"
One thing your wrote in your reply that is not clear I guess, or maybe you missed on that "The constructor doesn't creat any objects", ahhh well it DOES creat a J object when you call a constructor "new J()", if it wasn't creating a J object you wouldn't have been able to call the instance method m1(), right??
 
Alfred Kemety
Ranch Hand
Posts: 279
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well at the point that m2() is executed, the objects are not available for m2(), since they are local to m1(), that's true. YET, m1() didn't return yet, you're still in the body of m1() while you're executing m2(). Thus the third object is not illegible for GC yet at the point m2() is invoked and executed.
Hope I was clear
 
Bharathi Balasubramanian
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear as crystal..
Thanx
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alfred Kemety:
Well Dan, I guess I missed reading the question, since the header wasn't in the post, I thought the question would be typically "after the line new J().m1(); is executed"
so your answer is correct if it asks "right before executing m2()"
One thing your wrote in your reply that is not clear I guess, or maybe you missed on that "The constructor doesn't creat any objects", ahhh well it DOES creat a J object when you call a constructor "new J()", if it wasn't creating a J object you wouldn't have been able to call the instance method m1(), right??


Yes, I should have said that it doesn't create any objects of type I. Of course, it does create a J object.
 
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 see that i1 and i2 are eligible for g.c. but not i3, place
System.out.println(i1 + "\n" + i2); after the creation of i3, System.gc() within method2 , and a adds a finalize method like this for the I class:
public void finalize()
{System.out.println("finalizing " + this);
}
 
reply
    Bookmark Topic Watch Topic
  • New Topic