Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A Question about the .NET garbage collector when cyclic references exist

Tags:

.net

I'm aware that the .net garbage collector detects cyclic references but I was wondering whether cyclic references may cause objects to stay longer than necessary.

I have a cyclic reference in my ASP.NET app (an intended one for performance related reasons), can I get away with that?

Regards

like image 585
Waleed Eissa Avatar asked Dec 04 '22 16:12

Waleed Eissa


1 Answers

It doesn't take the CLR any longer to remove a cyclic reference than a non-cyclic reference. The CLR uses a combination of techniques to garbage collect. The quick version though is it starts with all rooted objects (objects on the stack, or held with a strong GC handle). Any object that is reachable from these objects is alive. Anything else will be collected. A cyclic reference won't affect the results of this algorithm other than the CLR has to make sure to check for them and not walk in circles

like image 167
JaredPar Avatar answered Jun 04 '23 17:06

JaredPar