When do strings and static fields get reclaimed by the garbage collector?
I'm asking this because I know static
s in ASP.NET are always live.
The garbage collector only collects objects that are not accessible. An object referenced by a static field is accessible as soon as its class is loaded, so it will obviously not get collected (unless of course the field is set to refer to something else, causing the original object to become potentially eligible for collection).
As for strings, it depends. Literal strings are interned and therefore always accessible. Otherwise, same rules apply as for any object.
Strings are objects and will be collected when un-referenced.
static fields usually keep a permanent reference to an object and thus keep those objects from being collected. But as long as you still need those objects that's quite alright.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With