Wrong average after third input
<p>Hey Guys, I am writing a performance monitor program that keeps track of different things, one of them being the Memory Available. I have been able to use this same code to find out the min,max, and average for the other Performance Counters, but this one keeps giving me the wrong Average.</p>

<p>The first average is correct, then I get really low numbers. So lets say the first two times i got 1500MB and 1400MB free Average would be 1450MB, then third time is 1600MB, then my output for Average would be something in the 700-800MB.</p>

<p>If you can please point me to the right direction, that would be appreciated! Thanks :)</p>

<pre><code>        //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 &gt; 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 &lt; 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));
</code></pre>

<p>Here is an output example:</p>

<p>Sunday, 24/10/2010 02:49:10 PM</p>

<p>Type    Last    Average Min Max</p>

<p>Memory  1319MB  1319MB  1319MB  1319MB</p>

<p>Sunday, 24/10/2010 02:49:40 PM</p>

<p>Type    Last    Average Min Max</p>

<p>Memory  1326MB  442MB   0MB 1326MB</p>
