An interesting memory leak. Does anyone know why?
foreach (int x in Enumerable.Range(0, 1_000_000)
.Select(async i => i))
{
}
GC.Collect();
Console.WriteLine(GC.GetTotalAllocatedBytes()); // 1036542160
foreach (int x in Enumerable.Range(0, 1_000_000)
.Select(async i => i))
{
}
GC.Collect();
Console.WriteLine(GC.GetTotalAllocatedBytes()); // 2072860704
foreach (int x in Enumerable.Range(0, 1_000_000)
.Select(async i => i))
{
}
GC.Collect();
Console.WriteLine(GC.GetTotalAllocatedBytes()); // 3109160008
Where:
static class SelectAsync
{
public static IEnumerable<TResult> Select<T, TResult>(
this IEnumerable<T> source, Func<T, Task<TResult>> selector) =>
source
.ToObservable()
.Select(value => Observable.FromAsync(() => selector(value)))
.Concat()
.ToEnumerable();
}
There's actually no memory leak.
GC.GetTotalAllocatedBytes
is a count of the bytes allocated over the lifetime of the process. Every time there's a heap allocation, this counter goes up.
What you want to use instead is GC.GetTotalMemory
.
If you see the deltas between your test values, you'll see that they're approximately the same.
You'll see some minor variations in your tests related to memory pressure.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.