29 using System.Collections.Generic;
31 using System.Reflection;
32 using System.Threading;
35 namespace OpenSim.Framework.Monitoring
40 public static class MemoryWatchdog
47 public static bool Enabled
49 get {
return m_enabled; }
54 if (value && !m_enabled)
55 UpdateLastRecord(GC.GetTotalMemory(
false), Util.EnvironmentTickCount());
60 private static bool m_enabled;
65 public static double AverageHeapAllocationRate
67 get {
if (m_samples.Count > 0)
return m_samples.Average();
else return 0; }
73 public static double LastHeapAllocationRate
75 get {
if (m_samples.Count > 0)
return m_samples.Last();
else return 0; }
85 private static int m_maxSamples = 24;
90 private static int m_lastUpdateTick;
95 private static long m_lastUpdateMemory;
105 private static Queue<double> m_samples =
new Queue<double>(m_maxSamples);
107 public static void Update()
109 int now = Util.EnvironmentTickCount();
110 long memoryNow = GC.GetTotalMemory(
false);
111 long memoryDiff = memoryNow - m_lastUpdateMemory;
115 if (m_samples.Count >= m_maxSamples)
118 double elapsed = Util.EnvironmentTickCountSubtract(now, m_lastUpdateTick);
125 m_samples.Enqueue(memoryDiff / (double)elapsed);
128 UpdateLastRecord(memoryNow, now);
131 private static void UpdateLastRecord(
long memoryNow,
int timeNow)
133 m_lastUpdateMemory = memoryNow;
134 m_lastUpdateTick = timeNow;