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 Collector and Static Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Garbage Collector and Static Variables" Watch "Garbage Collector and Static Variables" New topic
Author

Garbage Collector and Static Variables

Shivashankar Gurumurthy
Greenhorn

Joined: Feb 26, 2005
Posts: 12
Hi
I want to know answers for the following questions. Anybody please help me out in this regard.
1)How would garbage collection work on the static variables
2)Will the static variables be garbage collected at all?

Thanks
Shiva
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
Shiva,
Just now I was going through a SCJP forum and came through this thread.
Barry replied ..
When a class A is loaded an object is created representing the class. This object is an instance of the class Class. From this object you can obtain other objects representing the variables (both static fields and member fields) of the class A. All these objects are held in the heap. The only variables held on the stack are local variables of a method.

So now we should conclude that the static variables are GCed in the same way the instance variable is GCed.
The answer to the second part should be " every thing which resides on the heap are created and destroyed".


~Sanjeev Singh<br />SCJP 1.5
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
Mhhh. I'm still considering what you mean with "static variables" and "garbage collected".

Let's take a look at this class:

As soon as someone uses Foo.str, the class loader of the JVM will load the Foo-class. A String-object will be created (on the heap!) and str will be set to refer to this String-object. str itself is on the heap,too, but in the object representing the FOO-class.
As soon as someone would execute i.e.

the old String object (the String-object with content "Hello World") is free for garbage collection.

The variable str will be existent as long as the class representation object will reside in the heap. It will "disappear" when the class Foo is unloaded (main thread and all user threads terminated/JVM shuts down).
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Variables aren't gc'ed at all. Only objects are gc'ed. A reference variable can *reference* an object, but it *is* not the object.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Static variables are destroyed when the class is unloaded. This can occur when it's class loader is garbage collected.
Shivashankar Gurumurthy
Greenhorn

Joined: Feb 26, 2005
Posts: 12
Hi All
Thanks everyone for your prompt and patient replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collector and Static Variables
 
Similar Threads
Garbage Collection
Static reference and GC
Static variable
Garbage Collection
Static Var GC