diff --git a/doc/100-General/10-Changelog.md b/doc/100-General/10-Changelog.md index 03b13585..91796304 100644 --- a/doc/100-General/10-Changelog.md +++ b/doc/100-General/10-Changelog.md @@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic * [#480](https://github.com/Icinga/icinga-powershell-framework/pull/480) Fixes service locking during Icinga Agent upgrade and ensures errors on service management are caught and printed with internal error handling * [#483](https://github.com/Icinga/icinga-powershell-framework/issues/483) Fixes REST-Api SSL certificate lookup from the Icinga Agent, in case a custom hostname was used or in certain domain environments were domain is not matching DNS domain * [#490](https://github.com/Icinga/icinga-powershell-framework/pull/490) Fixes the command `Uninstall-IcingaComponent` for the `service` component which is not doing anything +* [#491](https://github.com/Icinga/icinga-powershell-framework/issues/491) Fixes GC collection with `Optimize-IcingaForWindowsMemory` for every incoming REST connection call * [#497](https://github.com/Icinga/icinga-powershell-framework/pull/497) Fixes loop sleep for idle REST-Api threads by replacing them with [BlockingCollection](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.blockingcollection-1?view=net-6.0) [ConcurrentQueue](https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=net-6.0) ### Enhancements diff --git a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 index 4134f990..ee31946e 100644 --- a/lib/core/framework/New-IcingaEnvironmentVariable.psm1 +++ b/lib/core/framework/New-IcingaEnvironmentVariable.psm1 @@ -62,5 +62,6 @@ function New-IcingaEnvironmentVariable() $Global:Icinga.Protected.Add('RunAsDaemon', $FALSE); $Global:Icinga.Protected.Add('Minimal', $FALSE); $Global:Icinga.Protected.Add('ThreadName', ''); + $Global:Icinga.Protected.Add('GarbageCollector', @{ }); } } diff --git a/lib/core/tools/Optimize-IcingaForWindowsMemory.psm1 b/lib/core/tools/Optimize-IcingaForWindowsMemory.psm1 index d37766ae..7a2ed8f8 100644 --- a/lib/core/tools/Optimize-IcingaForWindowsMemory.psm1 +++ b/lib/core/tools/Optimize-IcingaForWindowsMemory.psm1 @@ -11,6 +11,10 @@ properly .PARAMETER ClearErrorStack Also clears the current error stack to free additional memory +.PARAMETER SmartGC + Ensures that memory is not flushed whenever this function is called, but instead + every 30 attempts this function is called to reduce CPU load. Only works for + PowerShell sessions with "$Global:Icinga.Protected.ThreadName" being set .EXAMPLE Optimize-IcingaForWindowsMemory; .EXAMPLE @@ -21,9 +25,26 @@ function Optimize-IcingaForWindowsMemory() { param ( - [switch]$ClearErrorStack = $FALSE + [switch]$ClearErrorStack = $FALSE, + [switch]$SmartGC = $FALSE ); + if ([string]::IsNullOrEmpty($Global:Icinga.Protected.ThreadName) -eq $FALSE -And $SmartGC) { + if ($Global:Icinga.Protected.GarbageCollector.ContainsKey($Global:Icinga.Protected.ThreadName) -eq $FALSE) { + $Global:Icinga.Protected.GarbageCollector.Add($Global:Icinga.Protected.ThreadName, 0); + + return; + } else { + $Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] += 1; + } + + if ($Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] -le 30) { + return; + } + + $Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] = 0; + } + # Clear all errors within our error stack if ($ClearErrorStack) { $Error.Clear(); diff --git a/lib/daemon/Add-IcingaForWindowsDaemon.psm1 b/lib/daemon/Add-IcingaForWindowsDaemon.psm1 index 85d5225f..213633d1 100644 --- a/lib/daemon/Add-IcingaForWindowsDaemon.psm1 +++ b/lib/daemon/Add-IcingaForWindowsDaemon.psm1 @@ -20,9 +20,12 @@ function Add-IcingaForWindowsDaemon() } while ($TRUE) { - Start-Sleep -Seconds 1; + Start-Sleep -Seconds 10; # Handle possible threads being frozen Suspend-IcingaForWindowsFrozenThreads; + + # Force Icinga for Windows Garbage Collection + Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; } } diff --git a/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 b/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 index c1f6750c..b8c0f215 100644 --- a/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 +++ b/lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1 @@ -78,7 +78,7 @@ function New-IcingaForWindowsRESTApi() while ($TRUE) { # Force Icinga for Windows Garbage Collection - Optimize-IcingaForWindowsMemory -ClearErrorStack; + Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; $Connection = Open-IcingaTCPClientConnection ` -Client (New-IcingaTCPClient -Socket $Socket) ` diff --git a/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 b/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 index f141fd65..3e001042 100644 --- a/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 +++ b/lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1 @@ -101,6 +101,6 @@ function New-IcingaForWindowsRESTThread() Close-IcingaTCPConnection -Client $Connection.Client; # Force Icinga for Windows Garbage Collection - Optimize-IcingaForWindowsMemory -ClearErrorStack; + Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; } } diff --git a/lib/daemons/ServiceCheckDaemon/daemon/Add-IcingaServiceCheckDaemon.psm1 b/lib/daemons/ServiceCheckDaemon/daemon/Add-IcingaServiceCheckDaemon.psm1 index f06dd134..1ae48928 100644 --- a/lib/daemons/ServiceCheckDaemon/daemon/Add-IcingaServiceCheckDaemon.psm1 +++ b/lib/daemons/ServiceCheckDaemon/daemon/Add-IcingaServiceCheckDaemon.psm1 @@ -78,7 +78,8 @@ function Add-IcingaServiceCheckDaemon() } } - Optimize-IcingaForWindowsMemory; + # Force Icinga for Windows Garbage Collection + Optimize-IcingaForWindowsMemory -SmartGC; Start-Sleep -Seconds 10; } diff --git a/lib/daemons/ServiceCheckDaemon/task/Add-IcingaServiceCheckTask.psm1 b/lib/daemons/ServiceCheckDaemon/task/Add-IcingaServiceCheckTask.psm1 index a551fe26..6b218b88 100644 --- a/lib/daemons/ServiceCheckDaemon/task/Add-IcingaServiceCheckTask.psm1 +++ b/lib/daemons/ServiceCheckDaemon/task/Add-IcingaServiceCheckTask.psm1 @@ -36,7 +36,7 @@ function Add-IcingaServiceCheckTask() Clear-IcingaCheckSchedulerEnvironment; # Force Icinga for Windows Garbage Collection - Optimize-IcingaForWindowsMemory -ClearErrorStack; + Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; continue; } @@ -133,6 +133,6 @@ function Add-IcingaServiceCheckTask() # Reset certain values from the scheduler environment Clear-IcingaServiceCheckDaemonEnvironment; # Force Icinga for Windows Garbage Collection - Optimize-IcingaForWindowsMemory -ClearErrorStack; + Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC; } }