【发布时间】:2019-02-28 10:55:09
【问题描述】:
以下代码的内存占用是多少,例如堆上的对象数。
for (int i = 0; i < 10000; i++)
{
await MyMethod();
}
【问题讨论】:
-
您可以在 Visual Studio 中自行检查。看看here。
标签: c# async-await
以下代码的内存占用是多少,例如堆上的对象数。
for (int i = 0; i < 10000; i++)
{
await MyMethod();
}
【问题讨论】:
标签: c# async-await
这个问题可能是重复的。
但以下对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;
【讨论】: