gpt4 book ai didi

c# - Process.PrivateMemorySize64 在多次迭代中返回相同的值

转载 作者:行者123 更新时间:2023-11-30 14:10:03 25 4
gpt4 key购买 nike

此代码在每次迭代中返回相同的值:

var process = Process.GetCurrentProcess();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(process.PrivateMemorySize64 + Environment.NewLine);
}

// Output:
// 19853313
// 19853313
// 19853313
// 19853313
// ...

此代码返回不同的值:

for (int i = 0; i < 10; i++)
{
var process = Process.GetCurrentProcess();
Console.WriteLine(process.PrivateMemorySize64 + Environment.NewLine);
}

// Output:
// 19865600
// 20336640
// 20791296
// 21245952
// ...

Process.GetCurrentProcess() 是否拍摄内存值的快照?

MSDN 的 GetCurrentProcess页面是这样说的,但我不确定其含义是什么:

获取一个新的 Process 组件并将其与当前事件的进程相关联

最佳答案

您需要调用以下行以刷新它:

 process.Refresh();

这应该对你有用:

var process = Process.GetCurrentProcess();
for (int i = 0; i < 10; i++)
{
Console.WriteLine(process.PrivateMemorySize64 + Environment.NewLine);
process.Refresh();
}

我现在得到的输出:

26152960

26763264

27377664

27922432

28532736

29143040

29757440

30302208

30912512

31522816

来自 Process.PrivateMemorySize64 Property - MSDN在他们提供的示例中。

此外,来自Process.Refresh Method - MSDN ,这进一步解释:

After Refresh is called, the first request for information about each property causes the process component to obtain a new value from the associated process.

When a Process component is associated with a process resource, the property values of the Process are immediately populated according to the status of the associated process. If the information about the associated process subsequently changes, those changes are not reflected in the Process component's cached values. The Process component is a snapshot of the process resource at the time they are associated. To view the current values for the associated process, call the Refresh method.

参见 this StackOverflow Question有关什么是快照以及什么不是属性的一些附加信息。

关于c# - Process.PrivateMemorySize64 在多次迭代中返回相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25922486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com