【问题标题】:Memory taken by await objectsawait 对象占用的内存
【发布时间】:2019-02-28 10:55:09
【问题描述】:

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

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

【问题讨论】:

  • 您可以在 Visual Studio 中自行检查。看看here

标签: c# async-await


【解决方案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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    相关资源
    最近更新 更多