我想了解async/await
在垃圾回收本地变量方面的行为。在下面的示例中,我分配了大量内存并进入了一个显着的延迟。如代码所示,在await
之后未使用Buffer
。它会在等待期间被垃圾回收,还是内存会被占用整个函数的持续时间?
/// <summary>
/// How does async/await behave in relation to managed memory?
/// </summary>
public async Task<bool> AllocateMemoryAndWaitForAWhile() {
// Allocate a sizable amount of memory.
var Buffer = new byte[32 * 1024 * 1024];
// Show the length of the buffer (to avoid optimization removal).
System.Console.WriteLine(Buffer.Length);
// Await one minute for no apparent reason.
await Task.Delay(60000);
// Did 'Buffer' get freed by the garabage collector while waiting?
return true;
}