Пример #1
0
        /// <summary>
        /// Update RAM usage statistics
        /// </summary>
        private void UpdateRamUsage()
        {
            _logController.AddLog(new ApplicationLog("Updating RAM usage"));

            double total      = Convert.ToDouble(_info.TotalPhysicalMemory);
            double usage      = total - Convert.ToDouble(_info.AvailablePhysicalMemory);
            double percentage = usage / total * 100;

            if (double.IsNaN(total) || double.IsInfinity(total))
            {
                throw new ArgumentException(nameof(total));
            }
            if (double.IsNaN(usage) || double.IsInfinity(usage))
            {
                throw new ArgumentException(nameof(usage));
            }
            if (double.IsNaN(percentage) || double.IsInfinity(percentage))
            {
                throw new ArgumentException(nameof(percentage));
            }

            RamUsage newUsage = new RamUsage(usage, total, percentage);

            if (EnableRamStatistics)
            {
                if (MaxUsageHistoryCount != 0 && _ramUsageHistory.Count + 1 > MaxUsageHistoryCount)
                {
                    RamUsage removed = _ramUsageHistory[0];
                    _ramUsageHistory.Remove(removed);
                    RamUsageRemovedEvent?.Invoke(removed);
                }
            }
            else
            {
                ClearRamUsageHistory();
            }

            _ramUsageHistory.Add(newUsage);
            RamUsageAddedEvent?.Invoke(newUsage);

            if (percentage >= _autoOptimizeRamThreshold && AutoOptimizePercentage)
            {
                double diff = (DateTime.Now - _lastAutoOptimizeTime).TotalSeconds;
                if (diff > 10)
                {
#pragma warning disable 4014
                    ClearMemory();
#pragma warning restore 4014
                }
            }

            _logController.AddLog(new ApplicationLog("Finished updating RAM usage"));
        }
Пример #2
0
 /// <summary>
 /// Remove a RamUsage object from the list of RamUsage objects
 /// </summary>
 /// <param name="ramUsage">The RamUsage object that should be removed</param>
 internal void RemoveRamUsage(RamUsage ramUsage)
 {
     _ramUsageHistory.Remove(ramUsage);
     RamUsageRemovedEvent?.Invoke(ramUsage);
 }