4
class StaticTest{

public static SomeClass statVar = new SomeClass();

}

After this if we access StaticTest.statVar and assign some new objects at random times, then when all these objects will get garbage collected? Answer: In normal garbage collection time.

But what if this statVar has references to some instance variables(objects)?

Not clear?

Ok, static variables life time is until the class unloaded. In web applications we are initializing many things in static context. Incase if we are providing some object references to this static context but we are not releasing them, then how it gets garbage collected?

I would be very happy to discuss on this.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Ratnakar.class
  • 301
  • 5
  • 12
  • 1
    Your question is very unclear at the moment... it would really help if you'd provide a full, concrete example with a specific questoin. – Jon Skeet Aug 10 '12 at 19:54
  • What do you mean by "not releasing them"? If something references a value, it won't be GCed. If nothing references it, it may be. – Dave Newton Aug 10 '12 at 19:56
  • Thanks for all your answers. I have some design issue. For each request there are objects getting created. Context is same for all requests. If request objects are given reference by context(Static here) variables, so these objects never get garbage collected until server restart? correct? – Ratnakar.class Aug 11 '12 at 15:31

3 Answers3

6

Objects referenced by static variable will be garbage collected at the time of class unloading. So, what ever the objects being referenced by the static reference won't be GCed until class-unloaded (Because there is always a reachable reference to object in heap).

kosa
  • 65,990
  • 13
  • 130
  • 167
  • ... which, in many cases, is effectively never. See http://stackoverflow.com/questions/148681/unloading-classes-in-java – yshavit Aug 10 '12 at 19:55
  • The content is not static. The variable, that references the object is static, the references object itself isn't. – Andreas Dolk Aug 10 '12 at 19:55
  • 1
    @Andreas_D: I always type content there and you always highlight it. Next time I will make sure NO WORD CONTENT in answer. – kosa Aug 10 '12 at 19:56
  • :) (and *static variables* won't get garbage collected, because *variables* get never garbage collected - but I know what you want to say: if an object is only referenced by a static variable, then it will not be gc'd before the class is unloaded) – Andreas Dolk Aug 10 '12 at 20:01
  • @thinksteep - that's really just by chance, I'm not monitoring SO or users for buzzwords :D (+1 for your answer) – Andreas Dolk Aug 10 '12 at 20:21
  • @Andreas_D: That's ok. Your comment is perfectly valid. So, no worries. – kosa Aug 10 '12 at 20:27
  • @thinksteep I understood your answers. I am really interested to discuss on this. Please find my comment on my question. and provide your comments. – Ratnakar.class Aug 11 '12 at 15:36
  • 1
    Yes, that is why context is for very special purposes. May be you may consider using session (or) updating same attribute in context something like that? – kosa Aug 11 '12 at 22:55
  • @Nambari yes. we have to use request or session objects, instead of context(which I thought earlier). – Ratnakar.class Sep 03 '12 at 09:23
0

Think about the Objects in memory, not the variables. statVar is a reference to some object in memory. If you retain a reference to the same Object somewhere else, then the Object will not be GC'd until that reference is released. It doesn't matter if the class is unloaded and statVar goes away, because that's just another reference to an Object that still has references living. So, it will not be cleaned in that case.

Carl
  • 905
  • 5
  • 9
0

static object references are typically considered GC roots and whatever they point to (and whatever is tied up by those objects) will be considered live. If you want the object they refer to to be subject of a garbage collection you will need to clear the reference to them (and all other references as well of course).

If your class is no longer referenced and your JVM is set to collect unused classes, thinksteep's answer apply. I wouldn't hold my breath waiting for that moment.

As long as you keep the data referenced the answer is simply that they will not be cleared. That's the most fundamental part of the protocol when you have a GC.

As a side note, I rarely think it is a good idea to keep things in a static context in web-apps unless it is a singleton object or something which is shared by all users of that web-app. In that case, why would you want it to be cleaned up as long as your app server is running? It doesn't make sense.

Fredrik
  • 5,759
  • 2
  • 26
  • 32