Pretty simple yes/no question:
Hypothetically, I have a class with an instance member method. Say the method sleeps the thread for an hour.
I assign the instance member method to be run by ThreadPool.QueueUserWorkItem(InstanceMethod). Then I get rid of all other references to the instance.
Will the ThreadPool thread keep the instance alive or will it wake up and try to work in a destroyed/GCed class object instance?
6
Assuming release build with tiered compilation turned off, it will mostly depend on whether there are instance or instance member accesses in the method body after the point where Thread.Sleep
was called.
This is confirmed by this passage from the .NET Memory Analysis Document by the main maintainer of the GC Maoni Stephens:
Stack variables, especially for C# programs, are actually not talked
about very much. The reason is that JIT also does a good job realizing
when a stack variable is no longer in use. When a method is done, a
stack root is guaranteed to disappear. But even before that, JIT can
also figure out when a stack var isn’t needed anymore so will not
report to GC, even when a GC happens in the middle of a method. Note
that it is not the case in DEBUG builds.
Even though this
doesn’t seem like a stack variable we have ourselves declared, it is implicitly there on the stack – see this answer. So the above quote still applies in our case.
Example code to illustrate this makes use of the WeakReference
type which according to the docs:
Represents a weak reference, which references an object while still
allowing that object to be reclaimed by garbage collection.
class Foo {
int bar = 42;
public void InstanceMethod(object o) {
var wr = new WeakReference(this);
Thread.Sleep(1000);
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Console.WriteLine(wr.IsAlive); // False if below is commented / True otherwise
// Console.WriteLine(this.bar); //will keep the instance alive
}
}
var foo = new Foo();
var dedicatedThread = new Thread(foo.InstanceMethod);
dedicatedThread.Start();
dedicatedThread.Join(); // False
var lambdaThread = new Thread(() => {
var foo = new Foo();
foo.InstanceMethod(null);
});
lambdaThread.Start();
lambdaThread.Join(); // False
foo = new Foo();
ThreadPool.QueueUserWorkItem(new WaitCallback(foo.InstanceMethod));
// False
Thread.Sleep(3000);// Console.ReadLine
I’ve included multiple versions of offloading the instance method work to another thread – via lambda / and as a WaitCallback
delegate instance – the results are the same.
This shows that as Jeremy Lakeman commented the behavior that we notice for our class also applies to that of a delegate that holds the reference to our object and its instance method. That is when we do :
delegate.Invoke();
if the delegate isn’t used in the method that contains the code above does not need the delegate, it will be GCed and won’t hold our object alive by itself too.
9
Will the
ThreadPool
thread keep the instance alive or will it wake up and try to work in a destroyed/GCed class object instance?
Yes, the ThreadPool
will keep the instance alive. In order to be able to invoke its method, the ThreadPool
holds a reference to the instance until the method is invoked. The method will be invoked pretty quickly, normally instantaneously, not after an hour. After the ThreadPool
invokes the method, it no longer holds a reference to the instance. But all the code in the method will run, and all the objects needed by the code will be kept alive at least as long as they are needed.
There is a case that the code will not run to the end, which is the premature termination of the process (ThreadPool
threads are background threads, and so they don’t prevent a process from terminating).
1