-
Notifications
You must be signed in to change notification settings - Fork 20
Menu option to restart with increased memory #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 168 additions & 0 deletions
168
src/main/java/network/brightspots/rcv/ApplicationRestarter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| /* | ||
| * RCTab | ||
| * Copyright (c) 2017-2025 Bright Spots Developers. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| /* | ||
| * Purpose: Utility class for restarting the RCTab application with new JVM arguments. | ||
| * Design: Uses ProcessBuilder to launch a new instance with updated memory settings. | ||
| * Conditions: Always. | ||
| * Version history: see https://github.com/BrightSpots/rcv. | ||
| */ | ||
|
|
||
| package network.brightspots.rcv; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import javafx.application.Platform; | ||
|
|
||
| /** | ||
| * Utility class for restarting the RCTab application with new JVM arguments. | ||
| * Handles cross-platform restart mechanisms for Windows, macOS, and Linux. | ||
| */ | ||
| class ApplicationRestarter { | ||
|
|
||
| /** | ||
| * Restart the application with specified max heap size. | ||
| * Launches a new process with the updated -Xmx parameter and exits the current instance. | ||
| * | ||
| * @param maxHeapMb maximum heap size in megabytes | ||
| * @return true if restart initiated successfully, false otherwise | ||
| */ | ||
| static boolean restartWithMemory(long maxHeapMb) { | ||
| try { | ||
| // Build command to restart application | ||
| List<String> command = buildRestartCommand(maxHeapMb + "m"); | ||
| Logger.info("Restart command: %s", String.join(" ", command)); | ||
|
|
||
| // Start new process | ||
| ProcessBuilder builder = new ProcessBuilder(command); | ||
| // Inherit the working directory from current process | ||
| builder.directory(new File(System.getProperty("user.dir"))); | ||
| // Redirect error stream to output for debugging | ||
| builder.redirectErrorStream(true); | ||
|
|
||
| Process process = builder.start(); | ||
| Logger.info("New RCTab process started with PID " + process.pid()); | ||
|
|
||
| // Give the new process a moment to start before we exit | ||
| Thread.sleep(500); | ||
|
|
||
| // Exit current instance | ||
| Logger.info("Restarting RCTab with %d MB heap. Shutting down current instance...", | ||
| maxHeapMb); | ||
| Platform.exit(); | ||
| System.exit(0); | ||
|
|
||
| return true; | ||
| } catch (IOException e) { | ||
| Logger.severe("Failed to restart application (IO error): %s", e.getMessage()); | ||
| return false; | ||
| } catch (InterruptedException e) { | ||
| Logger.severe("Restart interrupted: %s", e.getMessage()); | ||
| Thread.currentThread().interrupt(); | ||
| return false; | ||
| } catch (Exception e) { | ||
| Logger.severe("Failed to restart application: %s", e.getMessage()); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get path to java executable. | ||
| * Checks JAVA_HOME first, then uses 'java' from PATH as fallback. | ||
| * | ||
| * @return path to java executable | ||
| */ | ||
| private static String getJavaExecutablePath() { | ||
| String javaHome = System.getProperty("java.home"); | ||
| if (javaHome != null && !javaHome.isEmpty()) { | ||
| String javaBin = javaHome + File.separator + "bin" + File.separator + "java"; | ||
| if (isWindows()) { | ||
| javaBin += ".exe"; | ||
| } | ||
| File javaFile = new File(javaBin); | ||
| if (javaFile.exists()) { | ||
| return javaBin; | ||
| } | ||
| Logger.warning("Java executable not found at %s, falling back to 'java' from PATH", | ||
| javaBin); | ||
| } | ||
| // Fallback to PATH | ||
| return isWindows() ? "java.exe" : "java"; | ||
| } | ||
|
|
||
| /** | ||
| * Build command line for restarting the application. | ||
| * Reconstructs: java -Xmx{mem}m --module-path {path} --module {module} | ||
| * | ||
| * @param maxHeapString maximum heap size string, e.g. "2048m" | ||
| * @return command as list of strings for ProcessBuilder | ||
| */ | ||
| public static List<String> buildRestartCommand(String maxHeapString) { | ||
| String javaPath = getJavaExecutablePath(); | ||
| List<String> command = new ArrayList<>(); | ||
|
|
||
| // Java executable | ||
| command.add(javaPath); | ||
|
|
||
| // Memory parameter | ||
| command.add("-Xmx" + maxHeapString); | ||
|
|
||
| // Get module path from current runtime | ||
| String modulePath = System.getProperty("jdk.module.path"); | ||
| if (modulePath != null && !modulePath.isEmpty()) { | ||
| command.add("--module-path"); | ||
| command.add(modulePath); | ||
| Logger.info("Using module path: %s", modulePath); | ||
| } else { | ||
| Logger.warning("jdk.module.path not set. Restart may not work correctly."); | ||
|
|
||
| // Try to use classpath as fallback | ||
| String classPath = System.getProperty("java.class.path"); | ||
| if (classPath != null && !classPath.isEmpty()) { | ||
| command.add("-cp"); | ||
| command.add(classPath); | ||
| Logger.info("Using classpath fallback: %s", classPath); | ||
| } | ||
| } | ||
|
|
||
| // Add module and main class | ||
| if (modulePath != null && !modulePath.isEmpty()) { | ||
| command.add("--module"); | ||
| command.add("network.brightspots.rcv/network.brightspots.rcv.Main"); | ||
| } else { | ||
| // If no module path, use direct class launch | ||
| command.add("network.brightspots.rcv.Main"); | ||
| } | ||
|
|
||
| return command; | ||
| } | ||
|
|
||
| /** | ||
| * Check if running on Windows. | ||
| * | ||
| * @return true if OS is Windows | ||
| */ | ||
| private static boolean isWindows() { | ||
| String osName = System.getProperty("os.name"); | ||
| return osName != null && osName.toLowerCase().contains("win"); | ||
| } | ||
|
|
||
| /** | ||
| * Check if running on macOS. | ||
| * | ||
| * @return true if OS is macOS | ||
| */ | ||
| @SuppressWarnings("unused") | ||
| private static boolean isMac() { | ||
| String osName = System.getProperty("os.name"); | ||
| return osName != null && osName.toLowerCase().contains("mac"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /* | ||
| * RCTab | ||
| * Copyright (c) 2017-2023 Bright Spots Developers. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
| */ | ||
|
|
||
| /* | ||
| * Purpose: Utility class for calculating system memory and determining optimal heap size. | ||
| * Design: Uses JMX to query system memory, applies 80% rule with 512MB rounding. | ||
| * Conditions: Always. | ||
| * Version history: see https://github.com/BrightSpots/rcv. | ||
| */ | ||
|
|
||
| package network.brightspots.rcv; | ||
|
|
||
| import com.sun.management.OperatingSystemMXBean; | ||
| import java.lang.management.ManagementFactory; | ||
|
|
||
| /** | ||
| * Utility class for managing memory-related calculations and system queries. | ||
| * Provides methods to determine system RAM, current JVM heap size, and calculate | ||
| * optimal heap allocation based on available system resources. | ||
| */ | ||
| class MemoryManager { | ||
|
|
||
| private static final long MEGABYTE = 1024L * 1024L; | ||
| private static final long CHUNK_SIZE_MB = 512L; | ||
| private static final double PERCENTAGE = 0.80; // 80% of total RAM | ||
|
|
||
| /** | ||
| * Get total physical memory in MB. | ||
| * Uses com.sun.management.OperatingSystemMXBean for cross-platform compatibility. | ||
| * | ||
| * @return total physical RAM in MB, or -1 if cannot determine | ||
| */ | ||
| static long getTotalPhysicalMemoryMb() { | ||
| try { | ||
| OperatingSystemMXBean osBean = | ||
| (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); | ||
| long totalMemoryBytes = osBean.getTotalPhysicalMemorySize(); | ||
| if (totalMemoryBytes > 0) { | ||
| return totalMemoryBytes / MEGABYTE; | ||
| } | ||
| } catch (Exception e) { | ||
| Logger.warning("Unable to determine total physical memory: %s", e.getMessage()); | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * Get current max heap size the JVM is running with. | ||
| * | ||
| * @return current max heap in MB | ||
| */ | ||
| static long getCurrentMaxHeapMb() { | ||
| return Runtime.getRuntime().maxMemory() / MEGABYTE; | ||
| } | ||
|
|
||
| /** | ||
| * Calculate recommended memory: 80% of RAM, rounded down to nearest 512MB. | ||
| * Examples: | ||
| * - 8GB (8192MB) RAM → 6144MB (6GB) | ||
| * - 16GB (16384MB) RAM → 12800MB (12.5GB) | ||
| * - 32GB (32768MB) RAM → 26112MB (25.5GB) | ||
| * | ||
| * @return recommended heap size in MB, or -1 if unable to determine | ||
| */ | ||
| static long calculateRecommendedMemoryMb() { | ||
| long totalMb = getTotalPhysicalMemoryMb(); | ||
| if (totalMb <= 0) { | ||
| Logger.warning("Cannot calculate recommended memory: total physical memory unknown"); | ||
| return -1; | ||
| } | ||
|
|
||
| long eightyPercent = (long) (totalMb * PERCENTAGE); | ||
| // Round down to nearest 512MB chunk | ||
| long recommended = (eightyPercent / CHUNK_SIZE_MB) * CHUNK_SIZE_MB; | ||
|
|
||
| Logger.info( | ||
| "Memory calculation: Total RAM = %d MB, 80%% = %d MB, Rounded = %d MB", | ||
| totalMb, eightyPercent, recommended); | ||
|
|
||
| return recommended; | ||
| } | ||
|
|
||
| /** | ||
| * Format memory size for display. | ||
| * | ||
| * @param memoryMb memory size in megabytes | ||
| * @return formatted string like "6144 MB (6.0 GB)" | ||
| */ | ||
| static String formatMemorySize(long memoryMb) { | ||
| double gb = memoryMb / 1024.0; | ||
| return String.format("%d MB (%.1f GB)", memoryMb, gb); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.