.NET 4.5 Async + Await Fire and forget about a potential memory leak?

I use the same code below for the non-critical "reboot and crash" operation in ASP.NET

private void SomeMethod()
{
    FireAndForgetAsync();
}

private async Task FireAndForgetAsync()
{
    // Simulate medium running IO blocking operation
    await Task.Delay(sleepTime).ConfigureAwait(false);

    // Simulating the rare scenario where an error occurs
    throw new Exception("An Error Occurred");
}

The exception is caught in the task returned by the async method, which prevents the unhandled exception from completely killing the application.

But will this code cause memory leaks? Because the returned task (and its exception) is not assigned / not available? My thoughts is that it will just be garbage collection if some .NET code does not contain a reference to the task.

+4
source share
2 answers

. , .

CLR, - .

, task.Wait() , , . . MSDN TPL.

( )

/ . SOF: () , Exception.

( I3arnon, .Net 4.5.)

+4

. ( ), . , , GCed.

( while (true) ), , , .


. "fire and forget" async-await .

+2

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


All Articles