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

object creation and garbage collection

 
Sandeep Shukla
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i create a object( say a hashmap object) , is that object lifecycle limited to the method call or is this object available for garbage collection.
another doubt which springs into my mind...what all objects are really eligible for garbage collection?
 
Jimmy Die
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

"An object is eligible when no live thread can reach it.

To reach and object, a live thread must have a live, reachable reference variable to that object. "

So if you disconnect your reference variable from your object from within the same method, then that reference variable should be eligible.

ie

public void someMethod() {

MyClass x = new MyClass();

MyClass x = null; // now the Object which x first
// refered to is "eligible" (no handle to find it)

}
 
Fisher Daniel
Ranch Hand
Posts: 582
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All objects are eligible to garbage collection when those are not referenced again by another objects.



Hope this helps..
Correct me if I am wrong...

thanks
daniel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic