• Post Reply Bookmark Topic Watch Topic
  • New Topic

GC and static variable.  RSS feed

 
jasmine alba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to GC a static variable?

Please explain in detail.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object is eligible for GC if it is not reachable by a live thread. It does not matter if the reference to the object is static.
 
jasmine alba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But in one of the books i came across a scenario.here it goes.

Q: I want to allocate an object and make sure that it
never is deallocated. Can I tell the garbage collector
to ignore an object?
A: No. There isn�t a mechanism for marking an
object as undeletable. You can instead create a static
member of a class, and store a reference to the object
in that. Static members are considered live objects.

so how would GC work for static members.
 
jasmine alba
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
gotcha
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dreams unlimited:
But in one of the books i came across a scenario.here it goes.

Q: I want to allocate an object and make sure that it
never is deallocated. Can I tell the garbage collector
to ignore an object?
A: No. There isn�t a mechanism for marking an
object as undeletable. You can instead create a static
member of a class, and store a reference to the object
in that. Static members are considered live objects.

so how would GC work for static members.


Hey Jasmine,

IMHO, it seems as thoug we are trying to keep a static reference to the object. This will be considered in use reference.

Talking about Garbage collection in a beginner forum is allowed, but how the referneces are handled by the JVM to implement GC is little out of scope, but however, you may want to read this GC Appendix from sun website.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keep in mind that, it is possible that the entire class is garbage collected and that static reference will be removed, therefore making the object eligible for garbage collection as well if that was the last reference.

Never seen it happen that a class is garbage collected though.
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Rob,

IMHO a class is unloaded, not Garbage collected. Please correct me if I am wrong.

It goes like this.

1. Instances are run-time representation of classes.
2. Instance can hold references.
3. Reference have a key to access actual objects.
4. An object is garbage collected, when the reference has lost the key to reach it.
5. A class is unloaded, the "how" and "when" are only known to the JVM. It unloads based on some space/time algorithms.
[ March 27, 2008: Message edited by: Ramaswamy Srinivasan ]
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Class is still an Object, so after it has been unloaded there are no more references and it can be garbage collected.

But you're right, Classes are unloaded first.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
A Class is still an Object


Well, no, not really, not in Java at least.

so after it has been unloaded there are no more references and it can be garbage collected.


A Java class my be unloaded when it's classloader would be eligible for garbage collection: http://java.sun.com/docs/books/jvms/second_edition/html/Concepts.doc.html#32202

What would it mean for class then to be gc'ed? Do you have any references?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by dreams unlimited:
But in one of the books i came across a scenario.here it goes.

Q: I want to allocate an object and make sure that it
never is deallocated. Can I tell the garbage collector
to ignore an object?
A: No. There isn�t a mechanism for marking an
object as undeletable. You can instead create a static
member of a class, and store a reference to the object
in that. Static members are considered live objects.

so how would GC work for static members.


The answer of that book is misleading, if not plain wrong. Static members aren't objects, and therefore also can't be considered live objects.

Now, a static member could be a *reference* to an object, and that object would, of course, be considered a live object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!