Common variables and garbage collection

I read the garbage collection in .NET, and I was hoping for some clarification. Therefore, as I understand it, if I declare a public class shared variable, GC will never get rid of it. Is it correct?

Also, then what of the private variables? Take the following example:

public class myClass private shared myString As String public sub ChangeString(newString As String) myString = newString end sub end class 

Would a shared variable now get GCed if there were no class instances? So what if I change ChangeString as a generic sub?

+4
source share
4 answers

So, as I understand it, if I declare a public class shared variable, GC will never get rid of it. Is it correct?

Nearly. GC will not clear the line referenced by your shared variable.

If, however, you call ChangeString with a new line, the line pointed to by myString will no longer be embedded in this link and may be eligible for GC. However, a new line (link to newString ) will now be embedded in the myString variable, preventing it from garbage collection.

Would a shared variable now get GCed if there were no class instances?

Not. A common variable is the roots of an object, since it belongs to the "type" of the class, not to any instances.

But what if I change ChangeString as a generic sub?

This will have no effect.

+5
source

Static (general) variables are always considered roots. Streaming static (shared) variables are the roots of the owning stream. Thus, no matter what applies to these variables, the GC will never be cleared.

Would a shared variable now get GCed if there were no class instances?

Thus, if a static constructor is ever called, the referent will persist through the corresponding lifetime (either a static process or a thread for a static thread). It does not matter whether there are instances of the class or not, the important thing is whether the static constructor was executed (and this is somewhat non-deterministic, you only know that if any static members are needed, they will be created until the first time they are needed).

But what if I change ChangeString as a generic sub?

It does not matter.

+2
source

A shared variable lives in the class itself, so you don't need an instance of the class for the variable to survive, so your string will not be garbage collected.

It does not matter if the variable is private, it will still not be garbage collected. It doesn’t matter if you use the sharded method or the instance method to set the variable.

Note: the garbage collector never collects variables, it only collects objects.

+2
source

Shared references to variables are introduced into the application domain. Thus, as long as the application domain is still loaded, any object referenced by this variable is still rooted and therefore the GC will not collect this object no matter how many instances of the containing class may or may not exist. However, if you change the link, as it can be done using ChangeString in your specific example, then the old object will no longer be embedded and will now have the right to collect.

Again, until you perform advanced management of the application domain, any object referenced by myString will not have permission to collect, unless, of course, you stop referencing this object by reassigning the variable myString .

+2
source

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


All Articles