2018-09-18 10:32:37 +08:00
From d6e66d04c997b32aa5563503de5a61f0b09a6507 Mon Sep 17 00:00:00 2001
2018-09-16 12:01:48 +08:00
From: Aikar <aikar@aikar.co>
Date: Sun, 16 Sep 2018 00:00:16 -0400
Subject: [PATCH] Fix major memory leaks in ExpiringMap
computeIfAbsent would leak as the entry
was never registered into the ttl map
This fixes that ,as well as redesigns cleaning to
not run on every manipulation, and instead to run clean
once per tick per expiring map.
diff --git a/src/main/java/net/minecraft/server/ExpiringMap.java b/src/main/java/net/minecraft/server/ExpiringMap.java
2018-09-16 15:21:52 +08:00
index 4006f5a69c..d64c143017 100644
2018-09-16 12:01:48 +08:00
--- a/src/main/java/net/minecraft/server/ExpiringMap.java
+++ b/src/main/java/net/minecraft/server/ExpiringMap.java
2018-09-16 14:57:22 +08:00
@@ -6,25 +6,120 @@ import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
2018-09-16 12:01:48 +08:00
import it.unimi.dsi.fastutil.longs.Long2LongMap.Entry;
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.util.Map;
2018-09-16 13:04:40 +08:00
+import java.util.function.BiFunction;
+import java.util.function.Function;
2018-09-16 12:01:48 +08:00
+import java.util.function.LongFunction;
public class ExpiringMap<T> extends Long2ObjectOpenHashMap<T> {
private final int a;
- private final Long2LongMap b = new Long2LongLinkedOpenHashMap();
+ private final Long2LongMap ttl = new Long2LongLinkedOpenHashMap(); // Paper
public ExpiringMap(int i, int j) {
super(i);
this.a = j;
}
- private void a(long i) {
- long j = SystemUtils.b();
- this.b.put(i, j);
- ObjectIterator objectiterator = this.b.long2LongEntrySet().iterator();
+ // Paper start
+ private synchronized void setAccess(long i) { a(i); } // Paper - OBFHELPER
+ private synchronized void a(long i) {
+ long j = System.currentTimeMillis(); // Paper
+ this.ttl.put(i, j);
+ if (!registered) {
+ registered = true;
+ MinecraftServer.getServer().expiringMaps.add(this);
+ }
+ }
+
+ @Override
2018-09-16 13:04:40 +08:00
+ public T compute(long l, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
+ setAccess(l);
+ return super.compute(l, biFunction);
+ }
+
+ @Override
+ public T putIfAbsent(long l, T t) {
+ setAccess(l);
+ return super.putIfAbsent(l, t);
+ }
+
+ @Override
+ public T computeIfPresent(long l, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
+ setAccess(l);
+ return super.computeIfPresent(l, biFunction);
+ }
+
+ @Override
+ public T computeIfAbsent(long l, LongFunction<? extends T> longFunction) {
2018-09-16 12:01:48 +08:00
+ setAccess(l);
+ return super.computeIfAbsent(l, longFunction);
+ }
2018-09-16 13:04:40 +08:00
+
+ @Override
+ public boolean replace(long l, T t, T v1) {
+ setAccess(l);
+ return super.replace(l, t, v1);
+ }
+
+ @Override
+ public T replace(long l, T t) {
+ setAccess(l);
+ return super.replace(l, t);
+ }
+
+ @Override
+ public T putIfAbsent(Long aLong, T t) {
+ setAccess(aLong);
+ return super.putIfAbsent(aLong, t);
+ }
+
+ @Override
+ public boolean replace(Long aLong, T t, T v1) {
+ setAccess(aLong);
+ return super.replace(aLong, t, v1);
+ }
+
+ @Override
+ public T replace(Long aLong, T t) {
+ setAccess(aLong);
+ return super.replace(aLong, t);
+ }
+
+ @Override
+ public T computeIfAbsent(Long aLong, Function<? super Long, ? extends T> function) {
+ setAccess(aLong);
+ return super.computeIfAbsent(aLong, function);
+ }
+
+ @Override
+ public T computeIfPresent(Long aLong, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
+ setAccess(aLong);
+ return super.computeIfPresent(aLong, biFunction);
+ }
+
+ @Override
+ public T compute(Long aLong, BiFunction<? super Long, ? super T, ? extends T> biFunction) {
+ setAccess(aLong);
2018-09-16 14:57:22 +08:00
+ return super.compute(aLong, biFunction);
+ }
+
+ @Override
+ public synchronized void clear() {
+ ttl.clear();
+ super.clear();
2018-09-16 13:04:40 +08:00
+ }
+
2018-09-16 12:01:48 +08:00
+ private boolean registered = false;
2018-09-16 14:57:22 +08:00
+ private boolean hasLeaked = false;
2018-09-16 12:01:48 +08:00
+
+ // Break clean to its own method to be ticked
+ synchronized boolean clean() {
+ long now = System.currentTimeMillis();
+ ObjectIterator<Long2LongMap.Entry> objectiterator = this.ttl.long2LongEntrySet().iterator(); // Paper
while(objectiterator.hasNext()) {
- Entry entry = (Entry)objectiterator.next();
- Object object = super.get(entry.getLongKey());
- if (j - entry.getLongValue() <= (long)this.a) {
+ Long2LongMap.Entry entry = objectiterator.next(); // Paper
+ T object = super.get(entry.getLongKey()); // Paper
+ if (now - entry.getLongValue() <= (long)this.a) {
break;
}
2018-09-16 14:57:22 +08:00
@@ -33,7 +128,25 @@ public class ExpiringMap<T> extends Long2ObjectOpenHashMap<T> {
2018-09-16 12:01:48 +08:00
objectiterator.remove();
}
}
-
2018-09-16 14:57:22 +08:00
+ int ttlSize = this.ttl.size();
+ int thisSize = this.size();
2018-09-16 15:21:52 +08:00
+ if (ttlSize < thisSize) {
2018-09-16 14:57:22 +08:00
+ if (!hasLeaked) { // log once
+ hasLeaked = true;
+ MinecraftServer.LOGGER.warn("WARNING: ExpiringMap desync (" + ttlSize + ":" + thisSize + ")- Memory leak risk! We will recover from this, but this means there is still a bug. Please do not open an issue about this. Mention it in Discord (we don't need everyone reporting the same thing)");
2018-09-16 12:01:48 +08:00
+ }
2018-09-16 14:57:22 +08:00
+ try {
+ for (Entry<T> entry : this.long2ObjectEntrySet()) {
+ ttl.putIfAbsent(entry.getLongKey(), now);
+ }
+ } catch (Exception e) { } // Ignore any como's
2018-09-16 12:01:48 +08:00
+ }
+ if (isEmpty()) {
+ registered = false;
+ return true;
+ }
+ return false;
+ // Paper end
}
protected boolean a(T var1) {
2018-09-16 15:21:52 +08:00
@@ -51,7 +164,7 @@ public class ExpiringMap<T> extends Long2ObjectOpenHashMap<T> {
}
public T get(long i) {
- this.a(i);
+ if (ttl.containsKey(i)) this.setAccess(i); // Paper
return (T)super.get(i);
}
2018-09-16 12:01:48 +08:00
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
2018-09-18 10:32:37 +08:00
index eb3fc836fb..81cda5df56 100644
2018-09-16 12:01:48 +08:00
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -155,6 +155,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
public int autosavePeriod;
public File bukkitDataPackFolder;
public CommandDispatcher vanillaCommandDispatcher;
+ public List<ExpiringMap> expiringMaps = java.util.Collections.synchronizedList(new java.util.ArrayList<>()); // PAper
// CraftBukkit end
// Spigot start
public static final int TPS = 20;
2018-09-18 10:32:37 +08:00
@@ -999,6 +1000,7 @@ public abstract class MinecraftServer implements IAsyncTaskHandler, IMojangStati
2018-09-16 12:01:48 +08:00
this.methodProfiler.e();
org.spigotmc.WatchdogThread.tick(); // Spigot
PaperLightingQueue.processQueue(startTime); // Paper
+ expiringMaps.removeIf(ExpiringMap::clean); // Paper
this.slackActivityAccountant.tickEnded(l); // Spigot
co.aikar.timings.TimingsManager.FULL_SERVER_TICK.stopTiming(); // Paper
}
--
2.19.0