From c66a780b782a68ce2cc035f6b921df86ab8dbfc5 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 9 Jun 2024 13:42:26 +0200 Subject: [PATCH] Add Miscellaneous#invokeGarbageCollector method --- .../sosengine/base/utility/Miscellaneous.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java index c9ac276a..e26f5dd7 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java +++ b/base/src/main/java/de/staropensource/sosengine/base/utility/Miscellaneous.java @@ -21,6 +21,8 @@ package de.staropensource.sosengine.base.utility; import org.jetbrains.annotations.NotNull; +import java.lang.ref.WeakReference; + /** * Contains smaller functions that don't fit into other utility classes. * @@ -47,4 +49,16 @@ public class Miscellaneous { public static String padNumbers(long number, int length) { return String.format("%0" + length + "d", number); } + + /** + * Forcefully invokes the garbage collector and blocks execution until finished. + * + * @since 1-alpha0 + */ + public static void invokeGarbageCollector() { + Object object = new Object(); + WeakReference weakReference = new WeakReference<>(object); + object = null; + while(weakReference.get() != null) System.gc(); + } }