Reputation: 51100
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.
Upvotes: 5
Views: 4589
Reputation: 1
what about something like this
private static List<T> list;
public static List<T> getList() {
if(list==null)
initList(); //private static method init list like you want
return list;
}
Upvotes: 0
Reputation: 1555
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:
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.
Upvotes: 3
Reputation: 12997
The original question:
When does garbage collection happen for a static class?
Answer: When your application exits.
Upvotes: 3
Reputation: 69997
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.
Upvotes: 2
Reputation: 8449
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.
Upvotes: 1
Reputation: 863
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.
Upvotes: 0
Reputation: 39887
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. :)
Upvotes: 3
Reputation: 3744
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.
Upvotes: 0
Reputation: 116314
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;
Upvotes: 8
Reputation: 19790
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?
Upvotes: 2