Garbage collector and static class, variable

One moment has surprised me since the last few days. I want to know how the garbage collector works with static classes, variables?

As we all know, the garbage collector keeps track of objects created in the application and automatically deletes them when they are no longer in use. For a static class, an object is not created and loaded into memory by the debug application.

So, does the garbage collector handle static classes?

+6
source share
2 answers

Static classes do not need to be removed, because they are not objects, and they do not consume memory (except for the actual class code loaded by the .NET framework). On the other hand, static fields in static classes are objects that consume memory. They will not be collected because they are available for the full life of the application. If you want to free memory, you must set the field to null so that the object pointed to by the field is granted the right to GC

+10
source

He will never release them. (for example, in WEB before restarting IIS).

They will never say, "No one is referring to me, therefore I am free."

and

you cannot do anything new in Static Class .... therefore its use is just an object of the class (and not an instance).

So .net has no chance and does not interfere with the GC.

+1
source

Source: https://habr.com/ru/post/901208/


All Articles