Will static members be ever collected by the garbage collector?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Objects referenced by static variables will only be garbage collected when the relevant
AppDomainis garbage collected. In client applications, there’s often just a singleAppDomainwhich lives for the duration of the process. (An exception is when the application uses a plug-in architecture – different plug-ins may be loaded in differentAppDomains and theAppDomainmay be unloaded later.)In ASP.NET, “
AppDomainrecycling” happens periodically (for various reasons) – when this occurs, and the static variables within thatAppDomainwill no longer act as GC roots, and thus won’t prevent objects being garbage collected.If you were worried about an object being garbage collected while you still had a reference to it via a static variable, though, you can relax. While you can access the object, it won’t be garbage collected.