Skip to content

Commit

Permalink
More efficient counter to check memory occupation
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco authored Nov 15, 2023
1 parent 5badd7c commit 9356d23
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit 9356d23

Please sign in to comment.