嘿,夥計們,我正在寫一個跟蹤不同事情的性能監視器程序,其中之一就是可用內存。我已經能夠使用這個相同的代碼來找出其他性能計數器的最小值,最大值和平均值,但是這個一直給我錯誤的平均值。第三次輸入後的平均值錯誤
第一個平均值是正確的,然後我得到非常低的數字。因此,讓我們說前兩次我得到1500MB和1400MB免費平均爲1450MB,然後第三次是1600MB,那麼我的平均產量將在700-800MB。
如果您可以請點我正確的方向,那將不勝感激!謝謝:)
//Move to and get the latest value in the performance counter for Memory Available Bytes
m.CurrentMemoryAvailableBytes = perfMemoryBytes.NextValue();
//Increase the count that will be used to divide the sum to get average
iCount++;
m.TotalMemoryAvailableBytes += m.CurrentMemoryAvailableBytes;
//Check if the current value is greater then the max for memory available bytes
if (m.CurrentMemoryAvailableBytes > m.MaxMemoryAvailableBytes)
m.MaxMemoryAvailableBytes = m.CurrentMemoryAvailableBytes;
//Initialize Min so its not stuck at 0
if (iCount == 1)
{
m.MinMemoryAvailableBytes = m.CurrentMemoryAvailableBytes;
}
else
{
//Check if the current value is less then the min for memory available bytes
if (m.CurrentMemoryAvailableBytes < m.MinMemoryAvailableBytes)
m.MinMemoryAvailableBytes = m.CurrentMemoryAvailableBytes;
}
addListView("Memory Available Bytes", Convert.ToInt16(m.CurrentMemoryAvailableBytes/1048576),
Convert.ToInt16((m.TotalMemoryAvailableBytes/iCount)/1048576), Convert.ToInt16(m.MinMemoryAvailableBytes/1048576),
Convert.ToInt16(m.MaxMemoryAvailableBytes/1048576));
下面是一個輸出例子:
週日,24/10/2010下午2時49分10秒
類型最後平均最小值最大值
內存1319MB 1319MB 1319MB 1319MB
週日,24/10/2010下午2點49分40秒
最新型平均最小值最大值
內存1326MB 442MB 0MB 1326MB
爲什麼1400和1500的平均值是14500而不是1450? – 2010-10-24 18:52:38
錯誤地增加了一個0 – 2010-10-24 18:56:58
您的輸出顯示Min將變爲0 ...這意味着您已經收集至少一個*值0,所以平均值下降並不奇怪。同樣,如果你可以提供一個簡短但完整的程序,這將有助於...你可能也想看看Math.Max和Math.Min方法來避免你的if語句。 – 2010-10-24 19:20:55