From 6c0d6ee0cf6ec5d76bba61687d9fb72e49172315 Mon Sep 17 00:00:00 2001 From: yushijinhun Date: Sun, 27 May 2018 11:04:39 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E9=9D=9E=E5=8F=8D=E5=B0=84?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=8E=B7=E5=8F=96=E6=80=BB=E7=89=A9=E7=90=86?= =?UTF-8?q?=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../jackhuang/hmcl/util/OperatingSystem.java | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/OperatingSystem.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/OperatingSystem.java index d84670492..30cbbf58f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/OperatingSystem.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/OperatingSystem.java @@ -24,6 +24,11 @@ import java.io.File; import java.lang.management.ManagementFactory; import java.nio.charset.Charset; import java.util.Locale; +import java.util.Optional; + +import javax.management.JMException; +import javax.management.MBeanServer; +import javax.management.ObjectName; /** * Represents the operating system. @@ -103,11 +108,9 @@ public enum OperatingSystem { else CURRENT_OS = UNKNOWN; - Object bytes = ReflectionHelper.call(ManagementFactory.getOperatingSystemMXBean(), "getTotalPhysicalMemorySize"); - if (bytes instanceof Long) - TOTAL_MEMORY = (int) (((Long) bytes) / 1024 / 1024); - else - TOTAL_MEMORY = 1024; + TOTAL_MEMORY = getTotalPhysicalMemorySize() + .map(bytes -> (int) (bytes / 1024 / 1024)) + .orElse(1024); SUGGESTED_MEMORY = (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128); @@ -117,6 +120,19 @@ public enum OperatingSystem { SYSTEM_ARCHITECTURE = arch; } + private static Optional getTotalPhysicalMemorySize() { + MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); + try { + Object attribute = mBeanServer.getAttribute(new ObjectName("java.lang", "type", "OperatingSystem"), "TotalPhysicalMemorySize"); + if (attribute instanceof Long) { + return Optional.of((Long) attribute); + } + } catch (JMException e) { + return Optional.empty(); + } + return Optional.empty(); + } + public static void setClipboard(String string) { ClipboardContent c = new ClipboardContent(); c.putString(string);