2010-10-24 19 views
0

嘿,夥計們,我正在寫一個跟蹤不同事情的性能監視器程序,其中之一就是可用內存。我已經能夠使用這個相同的代碼來找出其他性能計數器的最小值,最大值和平均值,但是這個一直給我錯誤的平均值。第三次輸入後的平均值錯誤

第一個平均值是正確的,然後我得到非常低的數字。因此,讓我們說前兩次我得到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

+0

爲什麼1400和1500的平均值是14500而不是1450? – 2010-10-24 18:52:38

+0

錯誤地增加了一個0 – 2010-10-24 18:56:58

+1

您的輸出顯示Min將變爲0 ...這意味着您已經收集至少一個*值0,所以平均值下降並不奇怪。同樣,如果你可以提供一個簡短但完整的程序,這將有助於...你可能也想看看Math.Max和Math.Min方法來避免你的if語句。 – 2010-10-24 19:20:55

回答

1

我的猜測是,你存儲字節總數的uint,和它的溢出...或者類似的東西,反正。沒有更多的代碼很難說。當你加起來的數字突然變小時,溢出是一個明顯的候選人。

如果你可以編寫一個簡短但完整的程序來演示這個問題,那就可以很容易地告訴問題究竟是什麼。

我不清楚爲什麼你打電話給Convert.ToInt16雖然 - 你爲什麼要限制你的信息?

+0

我得到了它的感謝。答案不在這篇文章中,而是原始答案的評論之一。我使用的if語句不正確,當刪除時,它會輸出正確的平均值。將研究其他方式初始化分鐘到正確的值。再次感謝! – 2010-10-24 21:07:44