Skip to content

Instantly share code, notes, and snippets.

@MrMicky-FR
Last active February 9, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrMicky-FR/24e878f399b0ba547ef6537bf463baee to your computer and use it in GitHub Desktop.
Save MrMicky-FR/24e878f399b0ba547ef6537bf463baee to your computer and use it in GitHub Desktop.
A simple ram selector in Java
package fr.mrmicky.ramselector;
import javax.swing.*;
import java.lang.management.ManagementFactory;
/**
* @author MrMicky
*/
@SuppressWarnings("serial")
public class RamSelector extends JFrame {
private static final boolean COMPATIBLE = System.getProperty("sun.arch.data.model").equals("64");
private JComboBox<String> ramBox = new JComboBox<>();
private long maxRam = 8;
@SuppressWarnings("restriction")
public RamSelector(String defaultRam) {
int ram = 1;
if (defaultRam != null) {
try {
ram = Integer.parseInt(defaultRam) - 1;
if (ram < 0) {
ram = 0;
}
} catch (NumberFormatException ignored) {
}
}
try {
maxRam = ((com.sun.management.OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean())
.getTotalPhysicalMemorySize() / 1024 / 1024 / 1024;
if (maxRam < 1) {
maxRam = 1;
}
} catch (Throwable ignored) {
}
getContentPane().setLayout(null);
setTitle("Settings");
setResizable(false);
setSize(250, 175);
setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
JLabel ramLabel = new JLabel("RAM: ", SwingConstants.CENTER);
ramLabel.setBounds(10, 67, 56, 25);
getContentPane().add(ramLabel);
// Replace with your version
JLabel versionLabel = new JLabel("Version: " + "1.0");
versionLabel.setBounds(10, 11, 224, 14);
getContentPane().add(versionLabel);
// Add your name
JLabel authorLabel = new JLabel("Author: ");
authorLabel.setBounds(10, 36, 224, 20);
getContentPane().add(authorLabel);
ramBox.setBounds(76, 67, 158, 25);
getContentPane().add(ramBox);
JButton okBtn = new JButton("OK");
okBtn.setBounds(10, 102, 224, 25);
okBtn.addActionListener(e -> setVisible(false));
getContentPane().add(okBtn);
for (int i = 1; i <= maxRam; i++) {
ramBox.addItem(" " + i + " Go");
}
ramBox.setSelectedIndex(ram < maxRam ? ram : 0);
}
public void display() {
if (!COMPATIBLE) {
JOptionPane.showMessageDialog(this, "You have a 32bits Java, you can't have more than 1024mo of ram",
"Java 32bits", JOptionPane.WARNING_MESSAGE);
return;
}
setLocationRelativeTo(null);
setVisible(true);
}
public int getRam() {
return COMPATIBLE ? ramBox.getSelectedIndex() + 1 : 1;
}
public void setRam(int index) {
ramBox.setSelectedIndex(index - 1);
}
public long getMaxRam() {
return maxRam;
}
public String[] getRamArguments() {
int ram = getRam() * 1024;
return new String[] { "-Xms" + (ram - 512) + "M", "-Xmx" + ram + "M" };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment