简体   繁体   English

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

[英]Process.PrivateMemorySize64 Returns The Same Value Over Multiple Iterations

This code returns the same value in every iteration: 此代码在每次迭代中返回相同的值:

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

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

This code returns different values: 此代码返回不同的值:

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

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

Does Process.GetCurrentProcess() take a snapshot of memory values? Process.GetCurrentProcess()是否记录内存值的快照?

MSDN's GetCurrentProcess page says this, but I'm not sure what the implications are: MSDN的GetCurrentProcess页面说明了这一点,但我不确定其含义是什么:

Gets a new Process component and associates it with the currently active process

You need to call the following line in order to refresh this: 您需要调用以下行来刷新它:

 process.Refresh();

This should work for you now then: 这应该适合你现在:

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

Output I'm now getting: 输出我现在得到:

26152960 26152960

26763264 26763264

27377664 27377664

27922432 27922432

28532736 28532736

29143040 29143040

29757440 29757440

30302208 30302208

30912512 30912512

31522816 31522816

From Process.PrivateMemorySize64 Property - MSDN in their supplied example. 来自Process.PrivateMemorySize64属性 - MSDN在其提供的示例中。

In addition, from Process.Refresh Method - MSDN , this is explained further: 另外,从Process.Refresh方法 - 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. 调用Refresh后,第一次请求有关每个属性的信息会导致流程组件从关联的流程中获取新值。

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. 当Process组件与流程资源相关联时,将根据关联流程的状态立即填充Process的属性值。 If the information about the associated process subsequently changes, those changes are not reflected in the Process component's cached values. 如果关联过程的信息随后发生更改,则这些更改不会反映在Process组件的缓存值中。 The Process component is a snapshot of the process resource at the time they are associated. Process组件是关联时进程资源的快照。 To view the current values for the associated process, call the Refresh method. 要查看关联进程的当前值,请调用Refresh方法。

See this StackOverflow Question for some additional information around what is a snapshot and what is not in terms of properties. 请参阅此StackOverflow问题 ,了解有关什么是快照以及什么不是属性的其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM