等待对象占用的内存

时间:2019-02-28 10:55:10

标签: c# async-await

以下代码的内存占用量是多少,例如堆上的对象数。

for (int i = 0; i < 10000; i++)
        {
            await MyMethod();
        }

1 个答案:

答案 0 :(得分:1)

这个问题可能是重复的。

但是以下对VirtualMemorySize64方法的调用将使您获得当前的内存大小(以字节为单位)。如果您包装了该操作,则要在此之前和之后监视并抓取快照,可以在此处计算出总增量。

文档链接here

using System.Diagnostics;

...

long start = Process.GetCurrentProcess().VirtualMemorySize64;

for (int i = 0; i < 10000; i++)
{
    await MyMethod();
}

long end = Process.GetCurrentProcess().VirtualMemorySize64;

// You can then get the total difference in bytes
long diff = end - start;