Compare commits

..

3 Commits

Author SHA1 Message Date
b808d48bd3 Excludes proxies when adding garbage collector flags
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
2022-02-10 01:51:47 +01:00
c3ea85c20b Adds garbage collection flags to improve server performance
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good
2022-02-10 01:47:10 +01:00
c83b5a2edd Adds minecraft server ram sizes up to 24 gigabytes 2022-02-10 01:46:25 +01:00
3 changed files with 82 additions and 18 deletions

View File

@ -370,17 +370,32 @@ public class Server {
private String getJavaCommand() { private String getJavaCommand() {
ServerLauncherController controller = ServerLauncherController.getInstance(); ServerLauncherController controller = ServerLauncherController.getInstance();
if (serverVersion.toLowerCase().contains("latest")) { if (versionAtLeast("1.17")) {
return controller.getJavaCommand(); return controller.getJavaCommand();
} else {
return controller.getOldJavaCommand();
}
}
/**
* Checks if the current server version is at least the given version
*
* @param version <p>The version to require</p>
* @return <p>True if the server version is at least the given version</p>
*/
private boolean versionAtLeast(String version) {
if (serverVersion.toLowerCase().contains("latest")) {
return true;
} else if (serverVersion.contains(".") && serverVersion.split("\\.").length >= 2) { } else if (serverVersion.contains(".") && serverVersion.split("\\.").length >= 2) {
try { try {
if (Integer.parseInt(serverVersion.split("\\.")[1]) >= 17) { return Integer.parseInt(serverVersion.split("\\.")[0]) >=
return controller.getJavaCommand(); Integer.parseInt(version.split("\\.")[0]) &&
} Integer.parseInt(serverVersion.split("\\.")[1]) >=
Integer.parseInt(version.split("\\.")[1]);
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
} }
} }
return controller.getOldJavaCommand(); return false;
} }
/** /**
@ -389,17 +404,7 @@ public class Server {
* @throws IOException <p>If the process cannot be started</p> * @throws IOException <p>If the process cannot be started</p>
*/ */
private void startServerProcess() throws IOException { private void startServerProcess() throws IOException {
ProcessBuilder builder; ProcessBuilder builder = new ProcessBuilder(generateServerProcessArguments());
String serverPath;
//Decide the path of the .jar file to be executed
if (type.getName().equals("Custom")) {
serverPath = this.path + File.separator + serverVersion;
} else {
serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar";
}
builder = new ProcessBuilder(getJavaCommand(), "-Xmx" + this.maxRam, "-Xms512M",
"-Djline.terminal=jline.UnsupportedTerminal", "-Dcom.mojang.eula.agree=true", "-jar", serverPath,
"nogui");
builder.directory(new File(this.path)); builder.directory(new File(this.path));
builder.redirectErrorStream(true); builder.redirectErrorStream(true);
this.process = builder.start(); this.process = builder.start();
@ -417,6 +422,64 @@ public class Server {
}, 10, 500, TimeUnit.MILLISECONDS); }, 10, 500, TimeUnit.MILLISECONDS);
} }
/**
* Generates the process arguments required for starting this Minecraft server
*
* @return <p>The process arguments required for starting this Minecraft server</p>
*/
private List<String> generateServerProcessArguments() {
//Decide the path of the .jar file to be executed
String serverPath;
if (type.getName().equalsIgnoreCase("Custom")) {
serverPath = this.path + File.separator + serverVersion;
} else {
serverPath = jarDirectory + this.type.getName() + serverVersion + ".jar";
}
List<String> processArguments = new ArrayList<>(20);
processArguments.add(getJavaCommand());
processArguments.add("-Xmx" + this.maxRam);
processArguments.add("-Xms512M");
if (versionAtLeast("1.8") && !getType().isProxy()) {
addGarbageCollectorFlags(processArguments);
}
processArguments.add("-Djline.terminal=jline.UnsupportedTerminal");
processArguments.add("-Dcom.mojang.eula.agree=true");
processArguments.add("-jar");
processArguments.add(serverPath);
processArguments.add("nogui");
return processArguments;
}
/**
* Adds garbage collector flags to the server process arguments to improve performance
*
* @param processArguments <p>The process arguments to add the flags to</p>
*/
private void addGarbageCollectorFlags(List<String> processArguments) {
processArguments.add("-XX:+UseG1GC");
processArguments.add("-XX:+ParallelRefProcEnabled");
processArguments.add("-XX:MaxGCPauseMillis=200");
processArguments.add("-XX:+UnlockExperimentalVMOptions");
processArguments.add("-XX:+DisableExplicitGC");
processArguments.add("-XX:+AlwaysPreTouch");
boolean excessiveRam = Integer.parseInt(this.maxRam.substring(0, this.maxRam.length() - 1)) > 11;
processArguments.add("-XX:G1NewSizePercent=" + (excessiveRam ? 40 : 30));
processArguments.add("-XX:G1MaxNewSizePercent=" + (excessiveRam ? 50 : 40));
processArguments.add("-XX:G1HeapRegionSize=" + (excessiveRam ? "16M" : "8M"));
processArguments.add("-XX:G1ReservePercent=" + (excessiveRam ? 15 : 20));
processArguments.add("-XX:G1HeapWastePercent=5");
processArguments.add("-XX:G1MixedGCCountTarget=4");
processArguments.add("-XX:InitiatingHeapOccupancyPercent=" + (excessiveRam ? 20 : 15));
processArguments.add("-XX:G1MixedGCLiveThresholdPercent=90");
processArguments.add("-XX:G1RSetUpdatingPauseTimePercent=5");
processArguments.add("-XX:SurvivorRatio=32");
processArguments.add("-XX:+PerfDisableSharedMem");
processArguments.add("-XX:MaxTenuringThreshold=1");
processArguments.add("-Dusing.aikars.flags=https://mcflags.emc.gs");
processArguments.add("-Daikars.new.flags=true");
}
/** /**
* Updates a single console with process output * Updates a single console with process output
* *
@ -580,4 +643,5 @@ public class Server {
this.getMaxRam() this.getMaxRam()
); );
} }
} }

View File

@ -16,7 +16,7 @@ public class ServerHandler {
* Available ram sizes. For ServerLauncherGUI combo * Available ram sizes. For ServerLauncherGUI combo
*/ */
private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G",
"11G", "12G", "13G", "14G", "15G", "16G"}; "11G", "12G", "13G", "14G", "15G", "16G", "17G", "18G", "19G", "20G", "21G", "22G", "23G", "24G"};
private static boolean stoppingServers = false; private static boolean stoppingServers = false;
private static final ServerLauncherGUI gui = Main.getController().getGUI(); private static final ServerLauncherGUI gui = Main.getController().getGUI();