I have a class MyClass that has a parameter of type MyThreadPool in the constructor (DI).
MyThreadPool is a singleton. The code is like the following:
Public Class MyClass
Private _threadPool as MyThreadPool
Public Sub New(threadPool as MyThreadPool)
_threadPool = threadPool
End Sub
End Class
Does holding the singleton MyThreadPool in a private field (_threadPool) in MyClass cause any instance of MyClass to stay in memory and become non collectable by the GC?
No, because the reference is from the instance of MyClass
to the instance of MyThreadPool
. That means that the instance of MyClass
would keep the instance of MyThreadPool
alive, but not the other way round. The instance of MyThreadPool
doesn't have a reference to the instance of MyClass
, so it can't keep it alive.
Think of the garbage collector following references, exploring every object it can get to. If nothing has a reference to the instance of MyClass
, it's eligible for garbage collection. You can't "get to" the instance of MyClass
from MyThreadPool
, so it's fine.
See more on this question at Stackoverflow