Add gc() function to force garbage collection

This commit is contained in:
JeremyStar™ 2024-04-30 20:52:53 +02:00
parent 77f7c34298
commit c9ef0d2d9a
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
4 changed files with 16 additions and 3 deletions

View file

@ -48,4 +48,4 @@ processResources {
filesMatching('plugin.yml') {
expand props
}
}
}

View file

@ -62,7 +62,7 @@ public final class Main extends JavaPlugin {
preinitialized = true;
}
// Initialize classes requiring Bukkit API + initialize plugin
// Initialize plugin
@Override
public void onEnable() {
if (failed || !preinitialized) {

View file

@ -69,7 +69,7 @@ public class PluginCommand extends CommandBase {
break;
case "gc":
sender.sendMessage(Miscellaneous.format(Translation.PLUGINCOMMAND_GARBAGECOLLECT));
System.gc();
Miscellaneous.gc();
break;
case "playerinfo":
if (sender instanceof ConsoleCommandSender) {

View file

@ -26,6 +26,7 @@ import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat;
import java.util.Objects;
@ -48,6 +49,18 @@ public class Miscellaneous {
}
}
// Ensures garbage collection
// Stolen from JLibs
// https://github.com/santhosh-tekuri/jlibs/blob/e001fb7286c84f456125d1be00fc9a7e3b128881/core/src/main/java/jlibs/core/lang/RuntimeUtil.java#L148
public static void gc(){
ObjHolder.logger.warn(Miscellaneous.class, "Forcing garbage collection");
Object obj = new Object();
WeakReference<Object> ref = new WeakReference<>(obj);
obj = null;
while(ref.get() != null)
System.gc();
}
// Data type conversion
/// Bytes <-> Mebibytes
public static double byteToMib(int bytes, boolean flatten) {