Static methods

Possible duplicates:
Garbage collection of static elements

The garbage collector will clean up static methods and static class

+1


a source to share


6 answers


Methods and classes are not cleaned up. Objects.



If you have a static object reference, the object in question will be cleared after the static exit goes out of scope (i.e. when the AppDomain is unloaded).

+6


a source


duplicate: Garbage collection of static elements

Also I can mention when OBJECT will be built. The method and classes will not be collected.



public class TestClass
{
   public static Hashtable h_object = new Hashtable();
}

TestClass.h_object = null; 
//* here it has no more references and it will be added to GC.

      

+1


a source


As far as I know, this happens when the AppDomain is collected (i.e. when the app is closed).

0


a source


If you are referring to static fields, then no, not necessary. They, by definition, remain until the end of the process (or AppDomain).

If you are referencing a local variable allocated by code in a static method, then yes, heap allocated memory will be cleaned up by GC

0


a source


static means that there is only one object of this type. the best example is the main method. it only exists once. so garbage collection will collect these objects too, but not automatically in the program, just at the end.

apart from what Lukas Shalkauskas said with "some_object". "some_other_obj" = null;

0


a source


No. Static classes will not be removed until the application domain is closed.

-1


a source







All Articles