Does a thread lingering within an instance member method keep the instance alive if all other references are dropped?

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật