Skip to content

Commit 4a65ccf

Browse files
committed
Use CollectionUsageThreshold, add cooldown
1 parent 078a802 commit 4a65ccf

File tree

1 file changed

+9
-3
lines changed
  • worldedit-core/src/main/java/com/fastasyncworldedit/core

1 file changed

+9
-3
lines changed

worldedit-core/src/main/java/com/fastasyncworldedit/core/Fawe.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.concurrent.LinkedBlockingQueue;
4444
import java.util.concurrent.ThreadPoolExecutor;
4545
import java.util.concurrent.TimeUnit;
46+
import java.util.concurrent.atomic.AtomicLong;
4647

4748
/**
4849
* [ WorldEdit action ]
@@ -413,26 +414,31 @@ private void setupMemoryListener() {
413414
final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
414415
final NotificationEmitter ne = (NotificationEmitter) memBean;
415416

417+
AtomicLong lastWarn = new AtomicLong(System.currentTimeMillis());
416418
ne.addNotificationListener((notification, handback) -> {
417419
final long heapSize = Runtime.getRuntime().totalMemory();
418420
final long heapMaxSize = Runtime.getRuntime().maxMemory();
419421
if (heapSize < heapMaxSize) {
420422
return;
421423
}
422-
LOGGER.warn("High memory usage detected, FAWE will attempt to slow operations to prevent a crash.");
424+
final long time = System.currentTimeMillis();
425+
if (time > lastWarn.get() + TimeUnit.SECONDS.toMillis(30)) {
426+
lastWarn.set(time);
427+
LOGGER.warn("High memory usage detected, FAWE will attempt to slow operations to prevent a crash.");
428+
}
423429
MemUtil.memoryLimitedTask();
424430
}, null, null);
425431

426432
final List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans();
427433
for (final MemoryPoolMXBean mp : memPools) {
428-
if (mp.isUsageThresholdSupported()) {
434+
if (mp.isCollectionUsageThresholdSupported()) {
429435
final MemoryUsage mu = mp.getUsage();
430436
final long max = mu.getMax();
431437
if (max < 0) {
432438
continue;
433439
}
434440
final long alert = (max * Settings.settings().MAX_MEMORY_PERCENT) / 100;
435-
mp.setUsageThreshold(alert);
441+
mp.setCollectionUsageThreshold(alert);
436442
}
437443
}
438444
} catch (Throwable ignored) {

0 commit comments

Comments
 (0)