Does the Garbage Collector destroy temporarily unreferenced objects during async calls in .NET?

后端 未结 5 1746
刺人心
刺人心 2020-12-11 03:20

Imagine that I will make an async call in .NET, i.e. HttpWebRequest.BeginGetResponse, and the HttpWebRequest object isn\'t referenced at a broader scope. Will the Garbage Co

5条回答
  •  鱼传尺愫
    2020-12-11 03:44

    No, the garbage collector won't cause you problems.

    Don't assume that because you don't have access to the object, the garbage collector is going to clean it up.

    The garbage collector starts with a number of "roots" - objects and references that are known reachable. Then, all the objects reachable from those roots are found, and everything else is collected.

    Each running thread - including the thread(s) that process the Async calls are included in the list of roots.

提交回复
热议问题