When local thread variables are freed in C #

In the following code:

public void f() { List l1<int> = new List<int>(); List l2<int> = new List<int>(); //.. populate l1 and l2 ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object state) { // use l1 and l2 // force gc.collect l1 and l2? })); //.. } 

l1 and l2 are local streaming very large lists. When do they get the right to collect garbage? When a thread executes executing a block, do they become appropriate?

Is it possible to force l1 and l2 garbage collection when a thread is executing with them?

thanks

+4
source share
1 answer

Firstly, calling GC.Collect provides only garbage collection. If something else is mentioned, then it still will not be collected.

As for your answer, I believe that they will be collected as soon as they are no longer referenced, which will require the delegate to be completed and no longer mentioned.

So, if this is a simple use, I believe the delegate will be cleared when it is completed, which will clear the lists

However, you may fall into a trap where the anonymous delegate will not be cleared, but I think ThreadPool should handle this. If not, you might be interested in this SO, especially Rory's answer.

+4
source

Source: https://habr.com/ru/post/1447130/


All Articles