Skip to content
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

Validate maximum memory allocation #436

Merged
merged 5 commits into from Nov 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 32 additions & 2 deletions launcher/ui/pages/global/JavaPage.cpp
Expand Up @@ -58,9 +58,8 @@ JavaPage::JavaPage(QWidget *parent) : QWidget(parent), ui(new Ui::JavaPage)
ui->setupUi(this);
ui->tabWidget->tabBar()->hide();

auto sysMiB = Sys::getSystemRam() / Sys::mebibyte;
ui->maxMemSpinBox->setMaximum(sysMiB);
loadSettings();
updateThresholds();
}

JavaPage::~JavaPage()
Expand Down Expand Up @@ -177,6 +176,11 @@ void JavaPage::on_javaTestBtn_clicked()
checker->run();
}

void JavaPage::on_maxMemSpinBox_valueChanged(int i)
{
updateThresholds();
}

void JavaPage::checkerFinished()
{
checker.reset();
Expand All @@ -186,3 +190,29 @@ void JavaPage::retranslate()
{
ui->retranslateUi(this);
}

void JavaPage::updateThresholds()
{
auto sysMiB = Sys::getSystemRam() / Sys::mebibyte;
unsigned int maxMem = ui->maxMemSpinBox->value();

QString iconName;

if (maxMem >= sysMiB) {
iconName = "status-bad";
ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation exceeds your system memory capacity."));
} else if (maxMem > (sysMiB * 0.9)) {
iconName = "status-yellow";
ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation approaches your system memory capacity."));
} else {
iconName = "status-good";
ui->labelMaxMemIcon->setToolTip("");
}

{
auto height = ui->labelMaxMemIcon->fontInfo().pixelSize();
QIcon icon = APPLICATION->getThemedIcon(iconName);
QPixmap pix = icon.pixmap(height, height);
ui->labelMaxMemIcon->setPixmap(pix);
}
}
3 changes: 3 additions & 0 deletions launcher/ui/pages/global/JavaPage.h
Expand Up @@ -76,6 +76,8 @@ class JavaPage : public QWidget, public BasePage
bool apply() override;
void retranslate() override;

void updateThresholds();

private:
void applySettings();
void loadSettings();
Expand All @@ -85,6 +87,7 @@ private
void on_javaDetectBtn_clicked();
void on_javaTestBtn_clicked();
void on_javaBrowseBtn_clicked();
void on_maxMemSpinBox_valueChanged(int i);
void checkerFinished();

private:
Expand Down
82 changes: 46 additions & 36 deletions launcher/ui/pages/global/JavaPage.ui
Expand Up @@ -44,50 +44,38 @@
<property name="title">
<string>Memory</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="1" column="1">
<widget class="QSpinBox" name="maxMemSpinBox">
<property name="toolTip">
<string>The maximum amount of memory Minecraft is allowed to use.</string>
</property>
<property name="suffix">
<string notr="true"> MiB</string>
</property>
<property name="minimum">
<number>128</number>
</property>
<property name="maximum">
<number>65536</number>
</property>
<property name="singleStep">
<number>128</number>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="1,0,0,0">
<item row="1" column="0">
<widget class="QLabel" name="labelMaxMem">
<property name="text">
<string>Ma&amp;ximum memory allocation:</string>
</property>
<property name="value">
<number>1024</number>
<property name="buddy">
<cstring>maxMemSpinBox</cstring>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labelMinMem">
<item row="2" column="0">
<widget class="QLabel" name="labelPermGen">
<property name="text">
<string>&amp;Minimum memory allocation:</string>
<string notr="true">&amp;PermGen:</string>
</property>
<property name="buddy">
<cstring>minMemSpinBox</cstring>
<cstring>permGenSpinBox</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelMaxMem">
<item row="0" column="0">
<widget class="QLabel" name="labelMinMem">
<property name="text">
<string>Ma&amp;ximum memory allocation:</string>
<string>&amp;Minimum memory allocation:</string>
</property>
<property name="buddy">
<cstring>maxMemSpinBox</cstring>
<cstring>minMemSpinBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<item row="0" column="2">
<widget class="QSpinBox" name="minMemSpinBox">
<property name="toolTip">
<string>The amount of memory Minecraft is started with.</string>
Expand All @@ -99,7 +87,7 @@
<number>128</number>
</property>
<property name="maximum">
<number>65536</number>
<number>1048576</number>
</property>
<property name="singleStep">
<number>128</number>
Expand All @@ -109,17 +97,29 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelPermGen">
<property name="text">
<string notr="true">&amp;PermGen:</string>
<item row="1" column="2">
<widget class="QSpinBox" name="maxMemSpinBox">
<property name="toolTip">
<string>The maximum amount of memory Minecraft is allowed to use.</string>
</property>
<property name="buddy">
<cstring>permGenSpinBox</cstring>
<property name="suffix">
<string notr="true"> MiB</string>
</property>
<property name="minimum">
<number>128</number>
</property>
<property name="maximum">
<number>1048576</number>
</property>
<property name="singleStep">
<number>128</number>
</property>
<property name="value">
<number>1024</number>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="2" column="2">
<widget class="QSpinBox" name="permGenSpinBox">
<property name="toolTip">
<string>The amount of memory available to store loaded Java classes.</string>
Expand All @@ -141,6 +141,16 @@
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QLabel" name="labelMaxMemIcon">
<property name="text">
<string/>
</property>
<property name="buddy">
<cstring>maxMemSpinBox</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down
35 changes: 33 additions & 2 deletions launcher/ui/pages/instance/InstanceSettingsPage.cpp
Expand Up @@ -59,12 +59,12 @@ InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent)
{
m_settings = inst->settings();
ui->setupUi(this);
auto sysMB = Sys::getSystemRam() / Sys::mebibyte;
ui->maxMemSpinBox->setMaximum(sysMB);

connect(ui->openGlobalJavaSettingsButton, &QCommandLinkButton::clicked, this, &InstanceSettingsPage::globalSettingsButtonClicked);
connect(APPLICATION, &Application::globalSettingsAboutToOpen, this, &InstanceSettingsPage::applySettings);
connect(APPLICATION, &Application::globalSettingsClosed, this, &InstanceSettingsPage::loadSettings);
loadSettings();
updateThresholds();
}

bool InstanceSettingsPage::shouldDisplay() const
Expand Down Expand Up @@ -437,6 +437,11 @@ void InstanceSettingsPage::on_javaTestBtn_clicked()
checker->run();
}

void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i)
{
updateThresholds();
}

void InstanceSettingsPage::checkerFinished()
{
checker.reset();
Expand All @@ -447,3 +452,29 @@ void InstanceSettingsPage::retranslate()
ui->retranslateUi(this);
ui->customCommands->retranslate(); // TODO: why is this seperate from the others?
}

void InstanceSettingsPage::updateThresholds()
{
auto sysMiB = Sys::getSystemRam() / Sys::mebibyte;
unsigned int maxMem = ui->maxMemSpinBox->value();

QString iconName;

if (maxMem >= sysMiB) {
iconName = "status-bad";
ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation exceeds your system memory capacity."));
} else if (maxMem > (sysMiB * 0.9)) {
iconName = "status-yellow";
ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation approaches your system memory capacity."));
} else {
iconName = "status-good";
ui->labelMaxMemIcon->setToolTip("");
}

{
auto height = ui->labelMaxMemIcon->fontInfo().pixelSize();
QIcon icon = APPLICATION->getThemedIcon(iconName);
QPixmap pix = icon.pixmap(height, height);
ui->labelMaxMemIcon->setPixmap(pix);
}
}
3 changes: 3 additions & 0 deletions launcher/ui/pages/instance/InstanceSettingsPage.h
Expand Up @@ -77,10 +77,13 @@ class InstanceSettingsPage : public QWidget, public BasePage
virtual bool shouldDisplay() const override;
void retranslate() override;

void updateThresholds();

private slots:
void on_javaDetectBtn_clicked();
void on_javaTestBtn_clicked();
void on_javaBrowseBtn_clicked();
void on_maxMemSpinBox_valueChanged(int i);

void applySettings();
void loadSettings();
Expand Down
67 changes: 40 additions & 27 deletions launcher/ui/pages/instance/InstanceSettingsPage.ui
Expand Up @@ -112,18 +112,39 @@
<property name="checked">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<layout class="QGridLayout" name="gridLayout_2" columnstretch="1,0,0,0">
<item row="2" column="0">
<widget class="QLabel" name="labelPermGen">
<property name="text">
<string notr="true">PermGen:</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="labelMinMem">
<property name="text">
<string>Minimum memory allocation:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="maxMemSpinBox">
<item row="1" column="0">
<widget class="QLabel" name="labelMaxMem">
<property name="text">
<string>Maximum memory allocation:</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QLabel" name="labelPermgenNote">
<property name="text">
<string>Note: Permgen is set automatically by Java 8 and later</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QSpinBox" name="minMemSpinBox">
<property name="toolTip">
<string>The maximum amount of memory Minecraft is allowed to use.</string>
<string>The amount of memory Minecraft is started with.</string>
</property>
<property name="suffix">
<string notr="true"> MiB</string>
Expand All @@ -132,20 +153,20 @@
<number>128</number>
</property>
<property name="maximum">
<number>65536</number>
<number>1048576</number>
</property>
<property name="singleStep">
<number>128</number>
</property>
<property name="value">
<number>1024</number>
<number>256</number>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="minMemSpinBox">
<item row="1" column="2">
<widget class="QSpinBox" name="maxMemSpinBox">
<property name="toolTip">
<string>The amount of memory Minecraft is started with.</string>
<string>The maximum amount of memory Minecraft is allowed to use.</string>
</property>
<property name="suffix">
<string notr="true"> MiB</string>
Expand All @@ -154,17 +175,17 @@
<number>128</number>
</property>
<property name="maximum">
<number>65536</number>
<number>1048576</number>
</property>
<property name="singleStep">
<number>128</number>
</property>
<property name="value">
<number>256</number>
<number>1024</number>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="2" column="2">
<widget class="QSpinBox" name="permGenSpinBox">
<property name="toolTip">
<string>The amount of memory available to store loaded Java classes.</string>
Expand All @@ -186,24 +207,16 @@
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="labelPermGen">
<item row="1" column="3">
<widget class="QLabel" name="labelMaxMemIcon">
<property name="text">
<string notr="true">PermGen:</string>
<string notr="true"/>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="labelMaxMem">
<property name="text">
<string>Maximum memory allocation:</string>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QLabel" name="labelPermgenNote">
<property name="text">
<string>Note: Permgen is set automatically by Java 8 and later</string>
<property name="buddy">
<cstring>maxMemSpinBox</cstring>
</property>
</widget>
</item>
Expand Down