public void OnNext(RecycleLimitInfo recycleLimitInfo) { // Pretty much copied from the old CacheManager.CollectInfrequently implementation // to keep things working exactly as before... though 'level' conveys the info we // need surrounding GC frequency if (recycleLimitInfo.TrimFrequency == RecycleLimitNotificationFrequency.High) { _lastTrimPercent = Math.Min(50, _lastTrimPercent + 10); } // if we're running into pressure infrequently, we may want to decrease the trim percentage else if (_lastTrimPercent > 10 && recycleLimitInfo.TrimFrequency == RecycleLimitNotificationFrequency.Low) { _lastTrimPercent = Math.Max(10, _lastTrimPercent - 10); } long trimmedOrExpired = HostingEnvironment.TrimCache(_lastTrimPercent); recycleLimitInfo.RequestGC = (trimmedOrExpired > 0); return; }
public void OnNext(LowPhysicalMemoryInfo lowMemoryInfo) { int percent = 0; int gen2Count = GC.CollectionCount(2); DateTime utcNow = DateTime.UtcNow; // has there been a Gen 2 Collection since the last trim? if (gen2Count != _lastTrimGen2Count) { // Old code used to check this, but if you look closely, when wired up in the default way, // the default memory monitor only triggers the LowMemoryAction under high pressure. So this // should always be true. //if (IsAboveHighPressure()) { // choose percent such that we don't repeat this for ~5 (TARGET_TOTAL_MEMORY_TRIM_INTERVAL) minutes, // but keep the percentage between 10 and 50. long ticksSinceTrim = utcNow.Subtract(_lastTrimTime).Ticks; if (ticksSinceTrim > 0) { percent = Math.Min(50, (int)((_lastTrimPercent * TARGET_TOTAL_MEMORY_TRIM_INTERVAL_TICKS) / ticksSinceTrim)); percent = Math.Max(MIN_TOTAL_MEMORY_TRIM_PERCENT, percent); } #if PERF SafeNativeMethods.OutputDebugString(String.Format("AspNetMemoryMonitor.GetPercentToTrim: percent={0:N}, lastTrimPercent={1:N}, secondsSinceTrim={2:N}\n", percent, _lastLowMemoryTrimPercent, ticksSinceTrim / TimeSpan.TicksPerSecond)); #endif HostingEnvironment.TrimCache(percent); _lastTrimGen2Count = gen2Count; _lastTrimTime = utcNow; _lastTrimPercent = percent; } }