File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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...

I agree. Here's the link:
subject: object creation and garbage collection
jQuery in Action, 3rd edition