From 4c8a65d8a365dd82c0cde0ab859447554a80bd9a Mon Sep 17 00:00:00 2001 From: sk89q Date: Mon, 3 Aug 2015 14:16:23 -0700 Subject: [PATCH] Set better memory defaults. --- .../com/skcraft/launcher/Configuration.java | 4 +-- .../java/com/skcraft/launcher/Launcher.java | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/launcher/src/main/java/com/skcraft/launcher/Configuration.java b/launcher/src/main/java/com/skcraft/launcher/Configuration.java index 8471e68..28ea116 100644 --- a/launcher/src/main/java/com/skcraft/launcher/Configuration.java +++ b/launcher/src/main/java/com/skcraft/launcher/Configuration.java @@ -25,8 +25,8 @@ public class Configuration { private String jvmPath; private String jvmArgs; private int minMemory = 1024; - private int maxMemory = 1024; - private int permGen = 128; + private int maxMemory = 0; // Updated in Launcher + private int permGen = 256; private int windowWidth = 854; private int widowHeight = 480; private boolean proxyEnabled = false; diff --git a/launcher/src/main/java/com/skcraft/launcher/Launcher.java b/launcher/src/main/java/com/skcraft/launcher/Launcher.java index 604ec54..f6d5cce 100644 --- a/launcher/src/main/java/com/skcraft/launcher/Launcher.java +++ b/launcher/src/main/java/com/skcraft/launcher/Launcher.java @@ -23,6 +23,7 @@ import com.skcraft.launcher.update.UpdateManager; import com.skcraft.launcher.util.HttpRequest; import com.skcraft.launcher.util.SharedLocale; import com.skcraft.launcher.util.SimpleLogFormatter; +import com.sun.management.OperatingSystemMXBean; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -35,6 +36,7 @@ import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.lang.management.ManagementFactory; import java.net.URL; import java.net.URLEncoder; import java.util.Locale; @@ -87,13 +89,14 @@ public final class Launcher { SharedLocale.loadBundle("com.skcraft.launcher.lang.Launcher", Locale.getDefault()); this.baseDir = baseDir; - this.properties = LauncherUtils.loadProperties(Launcher.class, - "launcher.properties", "com.skcraft.launcher.propertiesFile"); + this.properties = LauncherUtils.loadProperties(Launcher.class, "launcher.properties", "com.skcraft.launcher.propertiesFile"); this.instances = new InstanceList(this); this.assets = new AssetsRoot(new File(baseDir, "assets")); this.config = Persistence.load(new File(configDir, "config.json"), Configuration.class); this.accounts = Persistence.load(new File(configDir, "accounts.dat"), AccountList.class); + setDefaultConfig(); + if (accounts.getSize() > 0) { accounts.setSelectedItem(accounts.getElementAt(0)); } @@ -108,6 +111,30 @@ public final class Launcher { updateManager.checkForUpdate(); } + /** + * Updates any incorrect / unset configuration settings with defaults. + */ + public void setDefaultConfig() { + double configMax = config.getMaxMemory() / 1024.0; + double suggestedMax = 2; + double available = Double.MAX_VALUE; + + try { + OperatingSystemMXBean bean = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); + available = bean.getTotalPhysicalMemorySize() / 1024.0 / 1024.0 / 1024.0; + if (available <= 6) { + suggestedMax = available * 0.48; + } else { + suggestedMax = 4; + } + } catch (Exception ignored) { + } + + if (config.getMaxMemory() <= 0 || configMax >= available - 1) { + config.setMaxMemory((int) (suggestedMax * 1024)); + } + } + /** * Get the launcher version. *