This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes object creation and garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object creation and garbage collection" Watch "object creation and garbage collection" New topic

object creation and garbage collection

Sandeep Shukla

Joined: Oct 21, 2004
Posts: 5
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

Joined: Nov 20, 2003
Posts: 97

"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.


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)


Jimmy Die
Fisher Daniel
Ranch Hand

Joined: Sep 14, 2001
Posts: 582
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...

wood burning stoves
subject: object creation and garbage collection