Could a job link cause a memory leak?

Consider the following code snippet:

public void Do()
{
   ....
   Task.Delay(5000).ContinueWith(t => DoSomething());
   ....
}

Assume that the method Docompletes execution before the task completes Delayand that DoSomethingit is not canceled.

Does the fact that the reference to the Taskmethod returned ContinueWithis not supported cause some memory leak?

+4
source share
2 answers

The task will be executed in the thread pool thread, as soon as the task is completed, the thread will be returned to the pool for use by another task.

, ( 45 ).

, , , .

, , , . , , , .

, - .

+4

() , , ?

.

( ) ?
* .

: Promise (async) ().

  • Promise (, Task.Delay) , - , , ( Task.Delay Timer, Task, ).
  • ( RagtimeWilly) ( TaskScheduler), .

, , , .

5 Timer Promise Task, , , Task ( , ). 5 Timer Task.Delay, , , DoSomething .

DoSomething , ( ), , .


* ( ), , , GC. , :

static void Main()
{
    while (true)
    {
        Task.Delay(int.MaxValue);
    }
}

OutOfMemoryException , :

static void Main()
{
    while (true)
    {
        new Task(() => Thread.Sleep(int.MaxValue)); // No thread as the task isn't started.
        Task.Delay(-1); // No Timer as the delay is infinite.
    }
}
+4

All Articles