views:

683

answers:

9

I want to make some data easily available throughout my application. I have a few variables in a class with static data, which get written to and read from at several different points.

This worked fine for a while, but now at one point where I expect to be able to retrieve an ArrayList all I get is "null".

I am wondering if the static class has been sent to the "garbage collector". Not sure what's going on, if you can help that would be much appreciated.

+2  A: 

If you can call the function than the object won't be garbage collected away because there's still a reference to it. Are you storing a pointer to the arraylist or the object itself?

PoweRoy
The ArrayList itself
Ankur
+9  A: 

you can try to make it final, and recompile the code in order to see if some other class CHANGES the reference to null:

public class Global {

    public final static List<String> data = new ArrayList<String>();

}

this allow to write:

Global.data.add("foo");

but not:

Global.data = null;
dfa
Try this, a good advice. +1
Adeel Ansari
Or set a breakpoint in a debugger to trigger if the variable is ever set to null. Gives you a handy stacktrace and everything =)
mikek
This isn't a static class. This is a class that includes a static variable, which isn't the same thing.
UncleO
@uncleo: there are no static classes in Java.
Robert Munteanu
My mistake - the class with static variables is what I meant.
Ankur
@Robert: there are static classes in Java: class Foo { static class Bar { } }
dfa
@dfa: True, but not in the sense that the OP intended.
mikek
@Mike: of course, thats the reason he knocked the thing down in his first assumption.
Adeel Ansari
@Mike. Before the question was edited, OP talked about his static class. I wanted to make sure he wasn't confusing static classes with static variables. The "static" keyword is dreadfully overloaded, and leads to lots of confusion on SO. One might be led to think that there would only be one copy of a static class, like there is only one copy of a static variable, and I wanted to eliminate the possibility. (They are are newer feature of Java. There are even a lot of posters here who don't even know that static classes exist.)
UncleO
uncleo: What, you're saying that there are serious design misses in Java? Unheard of! ;)
mikek
A: 

The garbage collector will not dispose a static class by it's own. The garbage collector can only get active if there is no reference to the class. As long as you can call the class itself, there is a reference. Also you cannot definitely say when the garbage collector is getting active. It runs as some kind if idle-task in the background.

I would recommend you to add some debug trace messages to your static class, so that you can see when it's called. There must be some side effects changing your ArrayList at runtime.

Alexander
+3  A: 

I have a few variables in a static class, which get written to... at several different points.

As you are confessing yourself, so a null can be assigned to the variable at one or more of those different points. :)

Adeel Ansari
A: 

The static could be null if it's in a different thread. Otherwise, it should always be there if you don't tell it something else.

nandarya
+3  A: 

As Svetlio said, the GC only runs on objects on the heap which aren't referenced anywhere. This may take x number of cycles depending on which GC-strategy and VM you're using. Since static classes are indefinitely referenced, they'll never be GC'd.

So some issues remain:

  1. Could it be part of your program setting class to null? Can you set a debug flag to scream whenever your arraylist is set null?
  2. Does this happen at a specific time? At startup? After a while?
  3. A dumb question, but you don't run new on your static class at any time?

Lasty, as you said...static variables suck for anything other than constants :P A singleton configuration is really cheap and a lot better.

mikek
+1  A: 

Static classes aren't like static variables. It's just yet another use for the keyword "static". In this case, it indicates the class is top-level but declared within another class.

So that means you can instantiate more than one object of this class, unlike say a static variable, for which there is only one copy.

Are you instantiating more than one of these and expecting them to be the same one?

The GC doesn't sound like it is the issue. If the object were no longer there, you couldn't retrieve the ArrayList from it at all.

UncleO
the question was wrong
dfa
+2  A: 

Garbage collection for classes happens if its class loader gets garbage collected. See this question for example. There was a sort time in an earlier Java (I think in 1.2) where the static fields were not considered by the GC and lots of class unload/reload caused empty fields.

kd304
1.0/1.1 had a bug where classes could be garbage collected with a reachable class loader.
Tom Hawtin - tackline
+3  A: 

The original question:

When does garbage collection happen for a static class?

Answer: When your application exits.

aberrant80
Actually, it can happen when the class is no longer used. Though this is usually only when the class loader is cleaned up. ;)
Peter Lawrey