5

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.

3
  • 3
    I know global variables are bad.
    – Ankur
    Jun 17, 2009 at 6:54
  • 1
    What is a "static class"? Do you mean a class with static variables, or a nested inner class?
    – skaffman
    Jun 17, 2009 at 7:39
  • Sorry, you are right. A class with static variables and methods
    – Ankur
    Jun 17, 2009 at 8:35

10 Answers 10

8

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;
9
  • 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
    Jun 17, 2009 at 7:15
  • 1
    This isn't a static class. This is a class that includes a static variable, which isn't the same thing.
    – UncleO
    Jun 17, 2009 at 7:21
  • 3
    @uncleo: there are no static classes in Java. Jun 17, 2009 at 8:14
  • My mistake - the class with static variables is what I meant.
    – Ankur
    Jun 17, 2009 at 8:36
  • 3
    @Robert: there are static classes in Java: class Foo { static class Bar { } }
    – dfa
    Jun 17, 2009 at 9:20
3

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. :)

3

The original question:

When does garbage collection happen for a static class?

Answer: When your application exits.

1
  • Actually, it can happen when the class is no longer used. Though this is usually only when the class loader is cleaned up. ;) Jun 17, 2009 at 19:52
3

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.

2

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?

0
2

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.

1
  • 1.0/1.1 had a bug where classes could be garbage collected with a reachable class loader. Jun 17, 2009 at 13:41
1

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.

0
0

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.

0

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.

0

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.