Skip to content

Commit 9a2e793

Browse files
committed
Fix GC collection on every REST connection
1 parent 6838a4f commit 9a2e793

8 files changed

+34
-7
lines changed

doc/100-General/10-Changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Released closed milestones can be found on [GitHub](https://github.com/Icinga/ic
1919
* [#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
2020
* [#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
2121
* [#490](https://github.com/Icinga/icinga-powershell-framework/pull/490) Fixes the command `Uninstall-IcingaComponent` for the `service` component which is not doing anything
22+
* [#491](https://github.com/Icinga/icinga-powershell-framework/issues/491) Fixes GC collection with `Optimize-IcingaForWindowsMemory` for every incoming REST connection call
2223
* [#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)
2324

2425
### Enhancements

lib/core/framework/New-IcingaEnvironmentVariable.psm1

+1
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ function New-IcingaEnvironmentVariable()
6262
$Global:Icinga.Protected.Add('RunAsDaemon', $FALSE);
6363
$Global:Icinga.Protected.Add('Minimal', $FALSE);
6464
$Global:Icinga.Protected.Add('ThreadName', '');
65+
$Global:Icinga.Protected.Add('GarbageCollector', @{ });
6566
}
6667
}

lib/core/tools/Optimize-IcingaForWindowsMemory.psm1

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
properly
1212
.PARAMETER ClearErrorStack
1313
Also clears the current error stack to free additional memory
14+
.PARAMETER SmartGC
15+
Ensures that memory is not flushed whenever this function is called, but instead
16+
every 30 attempts this function is called to reduce CPU load. Only works for
17+
PowerShell sessions with "$Global:Icinga.Protected.ThreadName" being set
1418
.EXAMPLE
1519
Optimize-IcingaForWindowsMemory;
1620
.EXAMPLE
@@ -21,9 +25,26 @@
2125
function Optimize-IcingaForWindowsMemory()
2226
{
2327
param (
24-
[switch]$ClearErrorStack = $FALSE
28+
[switch]$ClearErrorStack = $FALSE,
29+
[switch]$SmartGC = $FALSE
2530
);
2631

32+
if ([string]::IsNullOrEmpty($Global:Icinga.Protected.ThreadName) -eq $FALSE -And $SmartGC) {
33+
if ($Global:Icinga.Protected.GarbageCollector.ContainsKey($Global:Icinga.Protected.ThreadName) -eq $FALSE) {
34+
$Global:Icinga.Protected.GarbageCollector.Add($Global:Icinga.Protected.ThreadName, 0);
35+
36+
return;
37+
} else {
38+
$Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] += 1;
39+
}
40+
41+
if ($Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] -le 30) {
42+
return;
43+
}
44+
45+
$Global:Icinga.Protected.GarbageCollector[$Global:Icinga.Protected.ThreadName] = 0;
46+
}
47+
2748
# Clear all errors within our error stack
2849
if ($ClearErrorStack) {
2950
$Error.Clear();

lib/daemon/Add-IcingaForWindowsDaemon.psm1

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ function Add-IcingaForWindowsDaemon()
2020
}
2121

2222
while ($TRUE) {
23-
Start-Sleep -Seconds 1;
23+
Start-Sleep -Seconds 10;
2424

2525
# Handle possible threads being frozen
2626
Suspend-IcingaForWindowsFrozenThreads;
27+
28+
# Force Icinga for Windows Garbage Collection
29+
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
2730
}
2831
}

lib/daemons/RestAPI/daemon/New-IcingaForWindowsRESTApi.psm1

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function New-IcingaForWindowsRESTApi()
7878
while ($TRUE) {
7979

8080
# Force Icinga for Windows Garbage Collection
81-
Optimize-IcingaForWindowsMemory -ClearErrorStack;
81+
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
8282

8383
$Connection = Open-IcingaTCPClientConnection `
8484
-Client (New-IcingaTCPClient -Socket $Socket) `

lib/daemons/RestAPI/threads/New-IcingaForWindowsRESTThread.psm1

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,6 @@ function New-IcingaForWindowsRESTThread()
101101
Close-IcingaTCPConnection -Client $Connection.Client;
102102

103103
# Force Icinga for Windows Garbage Collection
104-
Optimize-IcingaForWindowsMemory -ClearErrorStack;
104+
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
105105
}
106106
}

lib/daemons/ServiceCheckDaemon/daemon/Add-IcingaServiceCheckDaemon.psm1

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ function Add-IcingaServiceCheckDaemon()
7878
}
7979
}
8080

81-
Optimize-IcingaForWindowsMemory;
81+
# Force Icinga for Windows Garbage Collection
82+
Optimize-IcingaForWindowsMemory -SmartGC;
8283

8384
Start-Sleep -Seconds 10;
8485
}

lib/daemons/ServiceCheckDaemon/task/Add-IcingaServiceCheckTask.psm1

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Add-IcingaServiceCheckTask()
3636
Clear-IcingaCheckSchedulerEnvironment;
3737

3838
# Force Icinga for Windows Garbage Collection
39-
Optimize-IcingaForWindowsMemory -ClearErrorStack;
39+
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
4040

4141
continue;
4242
}
@@ -133,6 +133,6 @@ function Add-IcingaServiceCheckTask()
133133
# Reset certain values from the scheduler environment
134134
Clear-IcingaServiceCheckDaemonEnvironment;
135135
# Force Icinga for Windows Garbage Collection
136-
Optimize-IcingaForWindowsMemory -ClearErrorStack;
136+
Optimize-IcingaForWindowsMemory -ClearErrorStack -SmartGC;
137137
}
138138
}

0 commit comments

Comments
 (0)