File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When GC will collect Object instance refered by Static member of some other Class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When GC will collect Object instance refered by Static member of some other Class ? " Watch "When GC will collect Object instance refered by Static member of some other Class ? " New topic
Author

When GC will collect Object instance refered by Static member of some other Class ?

Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
Hi,

I am bit confused regarding the Garbage Collection of object instance which reference is hold by some static member. When will that object be collected by GC ?


Thanks in advance.

SCWCD 1.4, SCJP 1.4
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 440

we cannot directly say when the garbage collector will destroy the objects
when a running thread using that object is dead, then only there is a possibility that the objects gets destroyed
and also when the objects are null referenced then they can be destroyed
we can call the gc() method but it is merely a request and not the command

SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right
Simran Dass
Ranch Hand

Joined: Jan 09, 2010
Posts: 183

A static member is available as long as the class is loaded. YOur question
has set me thinking.
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

The Object will not get marked for the collection by the GC as long as its reference exists. In case of static it will be loaded till the thread running it is alive. To make it eligible for GC you can call a method which can set the reference to null.

HTH


My Blog SCJP 5 SCWCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When GC will collect Object instance refered by Static member of some other Class ?