From d9354263a7e63b3db7c90bbc672b137cb1923a6f Mon Sep 17 00:00:00 2001 From: KJP12 Date: Sun, 27 Nov 2022 08:05:06 -0600 Subject: [PATCH] Use a WeakHashMap in WorldAttached to prevent memory leaks --- .../simibubi/create/foundation/utility/WorldAttached.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/simibubi/create/foundation/utility/WorldAttached.java b/src/main/java/com/simibubi/create/foundation/utility/WorldAttached.java index 236d38d16..e526aa610 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/WorldAttached.java +++ b/src/main/java/com/simibubi/create/foundation/utility/WorldAttached.java @@ -2,9 +2,9 @@ package com.simibubi.create.foundation.utility; import java.lang.ref.WeakReference; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.WeakHashMap; import java.util.function.BiConsumer; import java.util.function.Consumer; import java.util.function.Function; @@ -22,7 +22,9 @@ public class WorldAttached { public WorldAttached(Function factory) { this.factory = factory; - attached = new HashMap<>(); + // Weak key hashmaps prevent worlds not existing anywhere else from leaking memory. + // This is only a fallback in the event that unload events fail to fire for any reason. + attached = new WeakHashMap<>(); allMaps.add(new WeakReference<>(attached)); }