From 9356d23b71cb7dc7ddbbbbbdb1f8d04b56b63ef9 Mon Sep 17 00:00:00 2001 From: Mario Fusco Date: Wed, 15 Nov 2023 09:25:24 +0100 Subject: [PATCH] More efficient counter to check memory occupation --- .../api/rulesengine/MemoryMonitorUtil.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/MemoryMonitorUtil.java b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/MemoryMonitorUtil.java index b1630e34..593be767 100644 --- a/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/MemoryMonitorUtil.java +++ b/drools-ansible-rulebook-integration-api/src/main/java/org/drools/ansible/rulebook/integration/api/rulesengine/MemoryMonitorUtil.java @@ -15,8 +15,9 @@ public class MemoryMonitorUtil { // check memory per configured number of events are consumed public static final String MEMORY_CHECK_EVENT_COUNT_THRESHOLD_PROPERTY = "drools.memory.check.event.count.threshold"; - private static final int DEFAULT_MEMORY_CHECK_EVENT_COUNT_THRESHOLD = 100; - private static final int MEMORY_CHECK_EVENT_COUNT_THRESHOLD; + + private static int COUNTER = 0; + private static final int COUNTER_MASK = (1 << 6) - 1; // 63 private static AtomicInteger eventCount = new AtomicInteger(0); @@ -43,14 +44,10 @@ private MemoryMonitorUtil() { } public static void checkMemoryOccupation() { - // check memory every N events to avoid performance overhead - if (eventCount.incrementAndGet() >= MEMORY_CHECK_EVENT_COUNT_THRESHOLD) { - doCheckMemoryOccupation(); - eventCount.set(0); + if ((COUNTER++ & COUNTER_MASK) == 0) { + // check memory occupation only once in 64 calls + return; } - } - - private static void doCheckMemoryOccupation() { int memoryOccupationPercentage = getMemoryOccupationPercentage(); if (memoryOccupationPercentage > MEMORY_OCCUPATION_PERCENTAGE_THRESHOLD) { // give GC a chance to free some memory