/// <summary> /// Starts the service. /// </summary> /// <returns>A task to indicate when the run has completed.</returns> public async Task StartAsync() { if (RuntimeEnvironment.OperatingSystemPlatform != Platform.Linux) { LogMessage($"{nameof(GarbageTruck)}NotSupported", $"{nameof(GarbageTruck)} does not support this platform."); return; } #if NETSTANDARD2_0 var memoryLimit = GetMemoryLimit(); #else var memoryLimit = await GetMemoryLimitAsync(this.cancellationToken); #endif LogMessage($"{nameof(GarbageTruck)}MemoryLimitDetected", $"{nameof(GarbageTruck)} Memory Limit", new { MemoryLimit = memoryLimit }); while (!this.cancellationToken.IsCancellationRequested) { #if NETSTANDARD2_0 long totalMemory = GetMemoryUsage(); #else long totalMemory = await GetMemoryUsageAsync(this.cancellationToken); #endif if (EnableMetricsLogging) { garbageLogger?.Count("GCTotalMemory", (int)totalMemory); } if (totalMemory > (long)(memoryLimit * CollectionThreshold)) { if (EnableMetricsLogging) { garbageLogger?.MoveGauge("GCCollect"); } GC.Collect(); } await Task.Delay(this.PollingPeriod); } }