Add gc() function to force garbage collection
This commit is contained in:
parent
77f7c34298
commit
c9ef0d2d9a
4 changed files with 16 additions and 3 deletions
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in a new issue