Add threadedGc() function

This commit is contained in:
JeremyStar™ 2024-04-30 21:44:59 +02:00
parent c9ef0d2d9a
commit 943750109c
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D

View file

@ -52,13 +52,18 @@ public class Miscellaneous {
// Ensures garbage collection // Ensures garbage collection
// Stolen from JLibs // Stolen from JLibs
// https://github.com/santhosh-tekuri/jlibs/blob/e001fb7286c84f456125d1be00fc9a7e3b128881/core/src/main/java/jlibs/core/lang/RuntimeUtil.java#L148 // https://github.com/santhosh-tekuri/jlibs/blob/e001fb7286c84f456125d1be00fc9a7e3b128881/core/src/main/java/jlibs/core/lang/RuntimeUtil.java#L148
public static void gc(){ public static void threadedGc(){
Thread gcThread = new Thread(Miscellaneous::gc);
gcThread.start();
}
public static void gc() {
ObjHolder.logger.warn(Miscellaneous.class, "Forcing garbage collection"); ObjHolder.logger.warn(Miscellaneous.class, "Forcing garbage collection");
Object obj = new Object(); Object obj = new Object();
WeakReference<Object> ref = new WeakReference<>(obj); WeakReference<Object> ref = new WeakReference<>(obj);
obj = null; obj = null;
while(ref.get() != null) while(ref.get() != null) System.gc();
System.gc(); ObjHolder.logger.warn(Miscellaneous.class, "Garbage collector finished");
} }
// Data type conversion // Data type conversion