From 4ce3dc1b764a17319923af8913b3c8032429b223 Mon Sep 17 00:00:00 2001
From: Spottedleaf <Spottedleaf@users.noreply.github.com>
Date: Thu, 30 May 2019 20:19:06 -0700
Subject: [PATCH] Fix memory leak if the server is constantly overloaded
 (#2109)

---
 ...ies-option-to-debug-dupe-uuid-issues.patch |  8 +++----
 .../Duplicate-UUID-Resolve-Option.patch       | 10 ++++-----
 ...more-aggressive-in-the-chunk-unload-.patch | 22 ++++++++++++++++++-
 .../PlayerNaturallySpawnCreaturesEvent.patch  |  2 +-
 4 files changed, 31 insertions(+), 11 deletions(-)

diff --git a/Spigot-Server-Patches/Add-Debug-Entities-option-to-debug-dupe-uuid-issues.patch b/Spigot-Server-Patches/Add-Debug-Entities-option-to-debug-dupe-uuid-issues.patch
index d1523945ad..b406e0313c 100644
--- a/Spigot-Server-Patches/Add-Debug-Entities-option-to-debug-dupe-uuid-issues.patch
+++ b/Spigot-Server-Patches/Add-Debug-Entities-option-to-debug-dupe-uuid-issues.patch
@@ -6,7 +6,7 @@ Subject: [PATCH] Add Debug Entities option to debug dupe uuid issues
 Add -Ddebug.entities=true to your JVM flags to gain more information
 
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 6b33c1c41..c97421721 100644
+index 6b33c1c418..c97421721e 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
@@ -19,7 +19,7 @@ index 6b33c1c41..c97421721 100644
          if (bukkitEntity == null) {
              bukkitEntity = CraftEntity.getEntity(world.getServer(), this);
 diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
-index 5d6d6a5a5..45ba357f8 100644
+index 81b82aeeef..446bdb0216 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
 @@ -0,0 +0,0 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
@@ -40,7 +40,7 @@ index 5d6d6a5a5..45ba357f8 100644
  
      protected void g() {
 diff --git a/src/main/java/net/minecraft/server/World.java b/src/main/java/net/minecraft/server/World.java
-index d7229f100..5cf3aea4f 100644
+index d7229f100b..5cf3aea4fc 100644
 --- a/src/main/java/net/minecraft/server/World.java
 +++ b/src/main/java/net/minecraft/server/World.java
 @@ -0,0 +0,0 @@ public abstract class World implements IIBlockAccess, GeneratorAccess, AutoClose
@@ -52,7 +52,7 @@ index d7229f100..5cf3aea4f 100644
      public boolean captureBlockStates = false;
      public boolean captureTreeGeneration = false;
 diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
-index 8cffbabfb..f6ba51d14 100644
+index 8cffbabfb6..f6ba51d14a 100644
 --- a/src/main/java/net/minecraft/server/WorldServer.java
 +++ b/src/main/java/net/minecraft/server/WorldServer.java
 @@ -0,0 +0,0 @@ public class WorldServer extends World {
diff --git a/Spigot-Server-Patches/Duplicate-UUID-Resolve-Option.patch b/Spigot-Server-Patches/Duplicate-UUID-Resolve-Option.patch
index 45d382f72a..c941b62460 100644
--- a/Spigot-Server-Patches/Duplicate-UUID-Resolve-Option.patch
+++ b/Spigot-Server-Patches/Duplicate-UUID-Resolve-Option.patch
@@ -33,7 +33,7 @@ But for those who are ok with leaving this inconsistent behavior, you may use WA
 It is recommended you regenerate the entities, as these were legit entities, and deserve your love.
 
 diff --git a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
-index ef882b897..385b3ac0c 100644
+index ef882b897f..385b3ac0ce 100644
 --- a/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
 +++ b/src/main/java/com/destroystokyo/paper/PaperWorldConfig.java
 @@ -0,0 +0,0 @@ public class PaperWorldConfig {
@@ -81,7 +81,7 @@ index ef882b897..385b3ac0c 100644
 +    }
  }
 diff --git a/src/main/java/net/minecraft/server/Chunk.java b/src/main/java/net/minecraft/server/Chunk.java
-index 197c0fe16..ef07f665b 100644
+index 197c0fe169..ef07f665b7 100644
 --- a/src/main/java/net/minecraft/server/Chunk.java
 +++ b/src/main/java/net/minecraft/server/Chunk.java
 @@ -0,0 +0,0 @@ public class Chunk implements IChunkAccess {
@@ -93,7 +93,7 @@ index 197c0fe16..ef07f665b 100644
  
          int k = MathHelper.floor(entity.locY / 16.0D);
 diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
-index 5711cf664..fffe26939 100644
+index 50c52118b8..5fa0dc8ac5 100644
 --- a/src/main/java/net/minecraft/server/Entity.java
 +++ b/src/main/java/net/minecraft/server/Entity.java
 @@ -0,0 +0,0 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
@@ -105,7 +105,7 @@ index 5711cf664..fffe26939 100644
          this.uniqueID = uuid;
          this.ap = this.uniqueID.toString();
 diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
-index 1362099f1..3fdb6935e 100644
+index 446bdb0216..276a365ff5 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
 @@ -0,0 +0,0 @@
@@ -194,7 +194,7 @@ index 1362099f1..3fdb6935e 100644
  
                      if (list != null) {
 diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
-index e61381221..e9e5ecf74 100644
+index e61381221f..e9e5ecf747 100644
 --- a/src/main/java/net/minecraft/server/WorldServer.java
 +++ b/src/main/java/net/minecraft/server/WorldServer.java
 @@ -0,0 +0,0 @@ package net.minecraft.server;
diff --git a/Spigot-Server-Patches/Make-targetSize-more-aggressive-in-the-chunk-unload-.patch b/Spigot-Server-Patches/Make-targetSize-more-aggressive-in-the-chunk-unload-.patch
index 7468444bdb..b045573ede 100644
--- a/Spigot-Server-Patches/Make-targetSize-more-aggressive-in-the-chunk-unload-.patch
+++ b/Spigot-Server-Patches/Make-targetSize-more-aggressive-in-the-chunk-unload-.patch
@@ -5,9 +5,18 @@ Subject: [PATCH] Make targetSize more aggressive in the chunk unload queue
 
 
 diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
-index ba59c561e8..6a49251cc7 100644
+index ff32fe32ce..74a1b4a852 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
+@@ -0,0 +0,0 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
+         this.v = new AtomicInteger();
+         this.playerMap = new PlayerMap();
+         this.trackedEntities = new Int2ObjectOpenHashMap();
+-        this.A = Queues.newConcurrentLinkedQueue();
++        this.A = new com.destroystokyo.paper.utils.CachedSizeConcurrentLinkedQueue<>(); // Paper
+         this.definedStructureManager = definedstructuremanager;
+         this.x = worldserver.getWorldProvider().getDimensionManager().a(file);
+         this.world = worldserver;
 @@ -0,0 +0,0 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
          // Spigot start
          org.spigotmc.SlackActivityAccountant activityAccountant = this.world.getMinecraftServer().slackActivityAccountant;
@@ -17,4 +26,15 @@ index ba59c561e8..6a49251cc7 100644
          // Spigot end
          while (longiterator.hasNext()) { // Spigot
              long j = longiterator.nextLong();
+@@ -0,0 +0,0 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {
+ 
+         Runnable runnable;
+ 
+-        while (booleansupplier.getAsBoolean() && (runnable = (Runnable) this.A.poll()) != null) {
++        int queueTarget = Math.min(this.A.size() - 100, (int) (this.A.size() * UNLOAD_QUEUE_RESIZE_FACTOR)); // Paper - Target this queue as well
++
++        while ((booleansupplier.getAsBoolean() || this.A.size() > queueTarget) && (runnable = (Runnable) this.A.poll()) != null) { // Paper - Target this queue as well
+             runnable.run();
+         }
+ 
 --
\ No newline at end of file
diff --git a/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch b/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
index 622452f110..b517e12132 100644
--- a/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
+++ b/Spigot-Server-Patches/PlayerNaturallySpawnCreaturesEvent.patch
@@ -9,7 +9,7 @@ from triggering monster spawns on a server.
 Also a highly more effecient way to blanket block spawns in a world
 
 diff --git a/src/main/java/net/minecraft/server/PlayerChunkMap.java b/src/main/java/net/minecraft/server/PlayerChunkMap.java
-index c2f442051..5d6d6a5a5 100644
+index 74a1b4a852..81b82aeeef 100644
 --- a/src/main/java/net/minecraft/server/PlayerChunkMap.java
 +++ b/src/main/java/net/minecraft/server/PlayerChunkMap.java
 @@ -0,0 +0,0 @@ public class PlayerChunkMap extends IChunkLoader implements PlayerChunk.d {