From 0337c1eb531c48bc730817e1e77714c24108985f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 2 Apr 2021 18:54:46 +0200 Subject: [PATCH] Add BauSystem shutdown after 5 minutes no player or 80 percentage memory load --- .../src/de/steamwar/bausystem/BauSystem.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index dd6e8c9..571a510 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -46,6 +46,7 @@ import org.bukkit.scheduler.BukkitTask; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.lang.management.ManagementFactory; import java.util.UUID; import java.util.logging.Level; @@ -171,19 +172,27 @@ public class BauSystem extends JavaPlugin implements Listener { if (autoShutdown != null) { autoShutdown.cancel(); } - /*autoShutdown = Bukkit.getScheduler().runTaskTimer(this, () -> { - try { - ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-c", "free"); - Process process = processBuilder.start(); - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); - bufferedReader.readLine(); - String s = bufferedReader.readLine(); - } catch (IOException exception) { - Bukkit.shutdown(); + autoShutdown = Bukkit.getScheduler().runTaskTimer(this, new Runnable() { + int count = 0; + + @Override + public void run() { + if (count >= 5) { + Bukkit.shutdown(); + return; + } + count++; + try { + long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize(); + long freeMemory = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getFreePhysicalMemorySize(); + if ((memorySize - freeMemory) / (double) memorySize > 0.8) { + Bukkit.shutdown(); + } + } catch (Throwable throwable) { + Bukkit.shutdown(); + } } - }, 1200, 1200);*/ - // (Runtime.getRuntime().maxMemory() - Runtime.getRuntime().freeMemory()) / Runtime.getRuntime().maxMemory() > 0.8 - autoShutdown = Bukkit.getScheduler().runTaskLater(this, Bukkit::shutdown, 6000); + }, 1200, 1200); } }