Garbage collector and static class, variable

戏子无情 提交于 2019-12-23 09:22:03

问题


One point is wondering in my mind since last few days. I want to know how Garbage collector work with static classes, variables?

As we all know Garbage collector keep track of objects that has been created in application and removed them automatically when they are no longer in use. For static class no object is created and it loaded in the memory with the application debug.

So Garbage collector handle static classes?


回答1:


Static classes don't need to be removed since they are not objects and they don't consume memory (other than the actual class code being loaded by the .NET infrastructure). On the other hand static fields in static classes are objects that consume memory. They won't be collected since they are accessible for the full lifetime of the application. If you want to free the memory you should set the field to null so that the object the field has been pointing to becomes eligible for GC




回答2:


He is never gonna release them. ( e.g. in WEB its until restart of the IIS).

They'll never say :" No one is referencing me so i'm free to go"

also

you can never do new to Static Class.... so its usages is merely by Class object ( not the instance).

So .net doesn't take any chances and keep it out of GC.



来源:https://stackoverflow.com/questions/8079887/garbage-collector-and-static-class-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!