Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 Collector destroy it and cause problems?

Example code:

using System;
using System.Net;

public class AsyncHttpWebRequest
{
    void Main()
    {
        var Request = HttpWebRequest.Create("http://www.contoso.com");
        var result = Request.BeginGetResponse(GetResponseCallback, null);
    }

    private void GetResponseCallback(IAsyncResult AsyncResult)
    {
        // Do Something..
    }
}

Alternate version (with the request being passed as an AsyncState):

using System;
using System.Net;

public class AsyncHttpWebRequest
{
    void Main()
    {
        var Request = HttpWebRequest.Create("http://www.contoso.com");
        var result = Request.BeginGetResponse(GetResponseCallback, Request);
    }

    private void GetResponseCallback(IAsyncResult AsyncResult)
    {
        // Do Something..
    }
}
like image 244
Jader Dias Avatar asked Jan 07 '09 18:01

Jader Dias


People also ask

WHO removes unreferenced objects?

Garbage collection makes Java memory efficient because it removes the unreferenced objects from heap memory and makes free space for new objects. The Java Virtual Machine has eight types of garbage collectors.

What does garbage collector in .NET do?

NET's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap.

What triggers garbage collection in C#?

Garbage collection occurs when the system is low on available physical memory or the GC. Collect() method is called explicitly in your application code. Objects that are no longer in use or are inaccessible from the root are candidates for garbage collection.

Does C# do automatic garbage collection?

The garbage collector serves as an automatic memory manager. You do not need to know how to allocate and release memory or manage the lifetime of the objects that use that memory. An allocation is made any time you declare an object with a “new” keyword or a value type is boxed.


2 Answers

An object is considered alive and non-eligible for garbage collection if any live thread contains a reference to it, or if it's referenced statically (directly or indirectly in both cases).

In both examples the async API keeps a reference to your request (within the thread pool where async IO operations are lodged) and so it won't be garbage collected until it completes.

like image 158
Drew Noakes Avatar answered Oct 13 '22 22:10

Drew Noakes


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.

like image 24
Bevan Avatar answered Oct 13 '22 22:10

Bevan