Add Miscellaneous#invokeGarbageCollector method

This commit is contained in:
JeremyStar™ 2024-06-09 13:42:26 +02:00
parent a6e83a25eb
commit c66a780b78
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -21,6 +21,8 @@ package de.staropensource.sosengine.base.utility;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.ref.WeakReference;
/** /**
* Contains smaller functions that don't fit into other utility classes. * 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) { public static String padNumbers(long number, int length) {
return String.format("%0" + length + "d", number); 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<Object> weakReference = new WeakReference<>(object);
object = null;
while(weakReference.get() != null) System.gc();
}
} }