Discussions

Performance and scalability: static classes and garbage collection

  1. static classes and garbage collection (6 messages)

    Hi By static classes I mean classes that contain static variables. I have a doubt on static classes and they being garbage collected. If I have a static variable with a reference to say a HashSet, then can it so happen that the class containing it will never get garbage collected for the life of the JVM ? I understand that if the hashset contains references to other classes then those referenced classes wont get garbage collected but am wondering what will happen to the class containing the hashset. Now for the sake of argument, if hundreds of instances of these static classes are created then will all those instances continue living on the heap (horror) just cause they all contain the static HashSet !!??
  2. Eh, your question is a bit unclear, but I'll take a wild guess that you mean a class like this public class Test { public static HashSet set = new HashSet(); } Am I correct? If so, you only get one "instance" of a class. Hence, the HashSet will live until you do Test.set = null; after which it (at some point) will be garbage collected. Cheers, Fredrik

  3. public class Test {

    private static HashSet set = new HashSet();
    }

    Am I correct? If so, you only get one "instance" of a class. Hence, the HashSet will live until you do

    Test.set = null;

    after which it (at some point) will be garbage collected. Huh see thats the easy part... I was refering to the Test class that we instantiate. Since this instance contains a static reference "set" wheteher this test instance will garbage collected or not was my question. So, in the same vein if we create a 100 such instances what happens ?
  4. I was refering to the Test class that we instantiate. Since this instance contains a static reference "set" wheteher this test instance will garbage collected or not was my question. So, in the same vein if we create a 100 such instances what happens ?
    Well, your subject is a bit misleading then. You're talking about static variables in instances. Since it's that way around, your instances will be garbage collected just fine - you should read up a bit on references in general. "set" however won't be garbage collected until it's set to null or the class itself is unloaded (by the classloader or by the JVM upon exit). Cheers, Fredrik
  5. As far as i guess Static variable means it is class variable.Even if we create 100 instances there was only one memory representation for that variable.So when ever u will make Test.set = null it will get collect into heap for reallocation of memory.
  6. For a static variable, there can NOT be more than one instance for that class. static YourClass yc = new YourClass(); creates an instance of the YourClass object and binds it to yc. Now, if you call yc = new YourClass() again, a new instance is created and bound to yc. However, the PREVIOUS INSTANCE is not bound to yc now and so WILL BE garbage-collected in DUE TIME. Does that make things clearer?
  7. Unless you tell the VM specifically noclassgc, it will unload them based on some time/space algorithmn. Not to mention in a J2EE environment(i.e. an app server), static classes behave slightly differently as you can have multiple classloaders, and multiple static instances. But finally, in a single classloader, you can't have more then one static instance of a given class. True however that the contents of the hashset don't get GC'ed unless the reference is null'ed. I think your issue is likely to do with the contents of the hashset.