File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Garbage collection of static data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage collection of static data" Watch "Garbage collection of static data" New topic
Author

Garbage collection of static data

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528

Can anybody please tell me when does static data are Garbage Collected ??

Thanks in advance .


If you want something you never had do something which you had never done
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40


First of all, please clarify what you mean by "static data"? The last time someone asked a question similar to this, my assumptions were wrong -- the question was about something else.


Now... assuming that this is about objects being referenced by static variables.... Objects are just objects. They can be referenced by instance variables. They can be referenced by static variables. And at the same time. And when none of those reachable variables reference the object anymore, the object will be eligible for GC.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Henry ,

Let me make your my query more clear .

For example consider an Utility class in a web application that returns a Session :



My question if i close the session on the implementation class will this be eligible for Garbage collection
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

My question if i close the session on the implementation class will this be eligible for Garbage collection


Objects are eligibile for garbage collection when the object is no longer reachable ... period.

The garbage collector doesn't have a concept of sessions or closing of them. The session object will be GC'ed when it is no longer reachable -- this means that the session factory must get rid of any references to it. And any code that has a reference to the object returned by the method must no longer reference it. Anything that is reachable, having a reference to the session object, will make the object not eligible to GC.

Henry
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thanks Henry , it was nice explanation .
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Garbage collection of static data