Regularly trigger GC to improve accuracy of metrics
@@ -28,6 +28,7 @@ import static java.util.stream.Collectors.toList;
class ActiveContainerDeactivationWatchdog implements ActiveContainerMetrics, AutoCloseable {
static final Duration WATCHDOG_FREQUENCY = Duration.ofMinutes(20);
static final Duration ACTIVE_CONTAINER_GRACE_PERIOD = Duration.ofHours(1);
+ static final Duration GC_TRIGGER_FREQUENCY = ACTIVE_CONTAINER_GRACE_PERIOD.minusMinutes(5);
private static final Logger log = Logger.getLogger(ActiveContainerDeactivationWatchdog.class.getName());
@@ -58,6 +59,11 @@ class ActiveContainerDeactivationWatchdog implements ActiveContainerMetrics, Aut
WATCHDOG_FREQUENCY.getSeconds(),
WATCHDOG_FREQUENCY.getSeconds(),
TimeUnit.SECONDS);
+ this.scheduler.scheduleWithFixedDelay(
+ ActiveContainerDeactivationWatchdog::triggerGc,
+ GC_TRIGGER_FREQUENCY.getSeconds(),
+ GC_TRIGGER_FREQUENCY.getSeconds(),
+ TimeUnit.SECONDS);
}
void onContainerActivation(ActiveContainer nextContainer) {
@@ -101,6 +107,13 @@ class ActiveContainerDeactivationWatchdog implements ActiveContainerMetrics, Aut
}
}
+ private static void triggerGc() {
+ // ActiveContainer has a finalizer, so gc -> finalizer -> gc is required.
+ System.gc();
+ System.runFinalization();
+ System.gc();
+ }
+
private List<DeactivatedContainer> getDeactivatedContainersSnapshot() {
Instant now = clock.instant();
synchronized (monitor) {
