wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC behavior towards static objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC behavior towards static objects" Watch "GC behavior towards static objects" New topic
Author

GC behavior towards static objects

Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Hi all,
I'm kind of curious to know about GC behavior towards static objects? Does GC collects static objects too and those objects that are initialized in static initializer or static methods?
For e.g.,
class A {
static { Object o = new Object();}
static void show() {Object p = new Object();}
}
In the following class, when will these two objects be available for GC? Does the normal rules apply here also.
Thanks,
Deep
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
An object cannot be static, but a reference to it can. An object is created on the heap and is eligible for garbage collection as soon as there is no more reference to it in the program.
In your code, the object referenced by o is eligible for GC at the end of the static block and the one referenced by p at the end of the static method show().
[ January 07, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Basanti Mathad
Ranch Hand

Joined: Aug 27, 2002
Posts: 60
can any one suggest a good material on GC.Am finding it hard to follow.I guess M.khalid is not enough.
Thank you.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Some discussions:
http://www.coderanch.com/t/240060/java-programmer-SCJP/certification/Garbage-Collection-Objects-loop
http://www.coderanch.com/t/239595/java-programmer-SCJP/certification/Garbage-collection-questions
http://www.coderanch.com/t/239961/java-programmer-SCJP/certification/GC-String-object
http://www.coderanch.com/t/240014/java-programmer-SCJP/certification/GC-WATCH-OUUUUUTTT-Examples
http://www.coderanch.com/t/239952/java-programmer-SCJP/certification/GC-WATCH-OUUUUUTTT
http://www.coderanch.com/t/240459/java-programmer-SCJP/certification/Finalize-GC
Very good article:
Reference Objects and Garbage Collection
Deep Chand
Ranch Hand

Joined: Dec 17, 2002
Posts: 133
Thanks for the detailed responses.
deep
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC behavior towards static objects
 
Similar Threads
Garbage Collector doubt
GC Doubt
array + GC
Doubt Garbage Collection
Garbage Collection problem-2