From efaf18a4f8a80f6f6975cf1852ee74ffc1b8e40c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 2 Apr 2021 19:05:02 +0200 Subject: [PATCH] Add RamUsage to be safe --- .../src/de/steamwar/bausystem/BauSystem.java | 4 +-- .../src/de/steamwar/bausystem/RamUsage.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/RamUsage.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java index 3b87d2c..6e3b6a9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/BauSystem.java @@ -170,9 +170,7 @@ public class BauSystem extends JavaPlugin implements Listener { } 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) { + if (RamUsage.getUsage() > 0.8) { Bukkit.shutdown(); } } catch (Throwable throwable) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/RamUsage.java b/BauSystem_Main/src/de/steamwar/bausystem/RamUsage.java new file mode 100644 index 0000000..aa98045 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/RamUsage.java @@ -0,0 +1,36 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem; + +import java.lang.management.ManagementFactory; + +public class RamUsage { + + public static double getUsage() { + try { + long memorySize = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getTotalPhysicalMemorySize(); + long freeMemory = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean()).getFreePhysicalMemorySize(); + return (memorySize - freeMemory) / (double) memorySize; + } catch (Throwable throwable) { + return 1D; + } + } + +}