Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

/**
* [ WorldEdit action ]
Expand Down Expand Up @@ -413,26 +414,31 @@ private void setupMemoryListener() {
final MemoryMXBean memBean = ManagementFactory.getMemoryMXBean();
final NotificationEmitter ne = (NotificationEmitter) memBean;

AtomicLong lastWarn = new AtomicLong(System.currentTimeMillis());
ne.addNotificationListener((notification, handback) -> {
final long heapSize = Runtime.getRuntime().totalMemory();
final long heapMaxSize = Runtime.getRuntime().maxMemory();
if (heapSize < heapMaxSize) {
return;
}
LOGGER.warn("High memory usage detected, FAWE will attempt to slow operations to prevent a crash.");
final long time = System.currentTimeMillis();
if (time > lastWarn.get() + TimeUnit.SECONDS.toMillis(30)) {
lastWarn.set(time);
LOGGER.warn("High memory usage detected, FAWE will attempt to slow operations to prevent a crash.");
}
MemUtil.memoryLimitedTask();
}, null, null);

final List<MemoryPoolMXBean> memPools = ManagementFactory.getMemoryPoolMXBeans();
for (final MemoryPoolMXBean mp : memPools) {
if (mp.isUsageThresholdSupported()) {
if (mp.isCollectionUsageThresholdSupported()) {
final MemoryUsage mu = mp.getUsage();
final long max = mu.getMax();
if (max < 0) {
continue;
}
final long alert = (max * Settings.settings().MAX_MEMORY_PERCENT) / 100;
mp.setUsageThreshold(alert);
mp.setCollectionUsageThreshold(alert);
}
}
} catch (Throwable ignored) {
Expand Down
Loading