private void AddHistoryItem(FixedSizeQueue <MemStat> from, int count, FixedSizeQueue <MemStat> to, long index)
        {
            long free  = 0;
            long total = 0;
            long used  = 0;

            for (int i = 1; i <= count; i++)
            {
                MemStat sample = from.ElementAt(history.Count() - i);
                free  += sample.Free;
                used  += sample.Used;
                total += sample.Total;
            }

            free  /= count;
            total /= count;
            used  /= count;

            var hourSample = new MemStat
            {
                SampleID     = index,
                Free         = free,
                Total        = total,
                Used         = used,
                PageInCount  = 0,
                PageOutCount = 0,
                SwapTotal    = 0,
                SwapUsed     = 0
            };

            to.Enqueue(hourSample);
        }
        public void Update(object sender)
        {
            lock (_lock)
            {
                historyIndex++;

                var status = new MEMORYSTATUSEX();
                GlobalMemoryStatusEx(status);
                long pageInSec  = Convert.ToInt64(_pageInSec.NextValue());
                long pageOutSec = Convert.ToInt64(_pageOutSec.NextValue());

                var ms = new MemStat
                {
                    SampleID     = historyIndex,
                    Free         = Convert.ToInt64(status.ullAvailPhys) / 1048576,
                    Total        = Convert.ToInt64(status.ullTotalPhys) / 1048576,
                    PageInCount  = pageInSec,  //sample time is 1 second so should be ~
                    PageOutCount = pageOutSec, //sample time is 1 second so should be ~
                    SwapTotal    = Convert.ToInt64(status.ullTotalPageFile) / 1048576,
                    SwapUsed     = Convert.ToInt64(status.ullTotalPageFile - status.ullAvailPageFile) / 1048576
                };

                ms.Used = ms.Total - ms.Free;
                history.Enqueue(ms);


                if (historyIndex >= 6 && (historyIndex % 6) == 0)
                {
                    historyIndexHour++;
                    AddHistoryItem(history, 6, historyHour, historyIndexHour);
                }
                if (historyIndex >= 144 && (historyIndex % 144) == 0)
                {
                    historyIndexDay++;
                    AddHistoryItem(history, 144, historyDay, historyIndexDay);
                }
            }
        }
        private void AddHistoryItem(FixedSizeQueue<MemStat> from, int count, FixedSizeQueue<MemStat> to, long index)
        {
            long free = 0;
            long total = 0;
            long used = 0;

            for (int i = 1; i <= count; i++)
            {
                MemStat sample = from.ElementAt(history.Count() - i);
                free += sample.Free;
                used += sample.Used;
                total += sample.Total;
            }

            free /= count;
            total /= count;
            used /= count;

            var hourSample = new MemStat
            {
                SampleID = index,
                Free = free,
                Total = total,
                Used = used,
                PageInCount = 0,
                PageOutCount = 0,
                SwapTotal = 0,
                SwapUsed = 0
            };
            to.Enqueue(hourSample);
        }
        public void Update(object sender)
        {
            lock (_lock)
            {
                historyIndex++;

                var status = new MEMORYSTATUSEX();
                GlobalMemoryStatusEx(status);
                long pageInSec = Convert.ToInt64(_pageInSec.NextValue());
                long pageOutSec = Convert.ToInt64(_pageOutSec.NextValue());

                var ms = new MemStat
                {
                    SampleID = historyIndex,
                    Free = Convert.ToInt64(status.ullAvailPhys) / 1048576,
                    Total = Convert.ToInt64(status.ullTotalPhys) / 1048576,
                    PageInCount = pageInSec, //sample time is 1 second so should be ~
                    PageOutCount = pageOutSec, //sample time is 1 second so should be ~
                    SwapTotal = Convert.ToInt64(status.ullTotalPageFile) / 1048576,
                    SwapUsed = Convert.ToInt64(status.ullTotalPageFile - status.ullAvailPageFile) / 1048576
                };

                ms.Used = ms.Total - ms.Free;
                history.Enqueue(ms);

                if (historyIndex >= 6 && (historyIndex % 6) == 0)
                {
                    historyIndexHour++;
                    AddHistoryItem(history, 6, historyHour, historyIndexHour);
                }
                if (historyIndex >= 144 && (historyIndex % 144) == 0)
                {
                    historyIndexDay++;
                    AddHistoryItem(history, 144, historyDay, historyIndexDay);
                }
            }
        }