Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage Collection Doubt

 
aresh babu
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we stop garbage collecting a user define object permanantly without assigning the references once again to that object?



Thanks in advance

 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My understanding:

An object is eligible for garbage collection as soon as there is no references to it from any live thread.

So, unless you override the finally method of that object and make a reference variable (in a live thread) point to it... it may get garbage collected.
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aresh babu wrote:How can we stop garbage collecting a user define object permanantly without assigning the references once again to that object?


You assign the object reference to a variable that's static and final, like

static final Object o = new Objcet(); // object will never be GCed
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francisco Montes wrote: . . . So, unless you override the finally method of that object and make a reference variable (in a live thread) point to it... it may get garbage collected.
By a "finally method," do you mean a finally block or a final reference?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Francisco Montes wrote: . . . So, unless you override the finally method of that object and make a reference variable (in a live thread) point to it... it may get garbage collected.
By a "finally method," do you mean a finally block or a final reference?

I'm guessing that Francisco meant the "finalize" method.
 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, I meant "finalize" (oops). Thanks Paul. :-)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic