From 5ee4fb3522ea2bb66ad49e1cdd91140b8d5c6a00 Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Fri, 11 Nov 2022 15:02:08 +0100 Subject: [PATCH] feat: validate maximum memory allocation Signed-off-by: Sefa Eyeoglu --- launcher/ui/pages/global/JavaPage.cpp | 25 +++- launcher/ui/pages/global/JavaPage.h | 3 + launcher/ui/pages/global/JavaPage.ui | 54 ++++---- .../pages/instance/InstanceSettingsPage.cpp | 26 +++- .../ui/pages/instance/InstanceSettingsPage.h | 3 + .../ui/pages/instance/InstanceSettingsPage.ui | 119 ++++++++++-------- 6 files changed, 151 insertions(+), 79 deletions(-) diff --git a/launcher/ui/pages/global/JavaPage.cpp b/launcher/ui/pages/global/JavaPage.cpp index 2cee15bf1..00c06cff9 100644 --- a/launcher/ui/pages/global/JavaPage.cpp +++ b/launcher/ui/pages/global/JavaPage.cpp @@ -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() @@ -177,6 +176,11 @@ void JavaPage::on_javaTestBtn_clicked() checker->run(); } +void JavaPage::on_maxMemSpinBox_valueChanged(int i) +{ + updateThresholds(); +} + void JavaPage::checkerFinished() { checker.reset(); @@ -186,3 +190,20 @@ void JavaPage::retranslate() { ui->retranslateUi(this); } + +void JavaPage::updateThresholds() +{ + auto sysMiB = Sys::getSystemRam() / Sys::mebibyte; + unsigned int maxMem = ui->maxMemSpinBox->value(); + + if (maxMem >= sysMiB) { + ui->labelMaxMemIcon->setText(u8"✘"); + ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation exceeds your system memory capacity.")); + } else if (maxMem > (sysMiB * 0.9)) { + ui->labelMaxMemIcon->setText(u8"⚠"); + ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation approaches your system memory capacity.")); + } else { + ui->labelMaxMemIcon->setText(u8"✔"); + ui->labelMaxMemIcon->setToolTip(""); + } +} diff --git a/launcher/ui/pages/global/JavaPage.h b/launcher/ui/pages/global/JavaPage.h index 64d4098e5..2ef6d7493 100644 --- a/launcher/ui/pages/global/JavaPage.h +++ b/launcher/ui/pages/global/JavaPage.h @@ -76,6 +76,8 @@ public: bool apply() override; void retranslate() override; + void updateThresholds(); + private: void applySettings(); void loadSettings(); @@ -85,6 +87,7 @@ slots: void on_javaDetectBtn_clicked(); void on_javaTestBtn_clicked(); void on_javaBrowseBtn_clicked(); + void on_maxMemSpinBox_valueChanged(int i); void checkerFinished(); private: diff --git a/launcher/ui/pages/global/JavaPage.ui b/launcher/ui/pages/global/JavaPage.ui index 6ccffed4d..19e23eba9 100644 --- a/launcher/ui/pages/global/JavaPage.ui +++ b/launcher/ui/pages/global/JavaPage.ui @@ -44,8 +44,8 @@ Memory - - + + The maximum amount of memory Minecraft is allowed to use. @@ -67,27 +67,17 @@ - - + + - &Minimum memory allocation: + &PermGen: - minMemSpinBox + permGenSpinBox - - - - Ma&ximum memory allocation: - - - maxMemSpinBox - - - - + The amount of memory Minecraft is started with. @@ -109,17 +99,27 @@ - - + + - &PermGen: + Ma&ximum memory allocation: - permGenSpinBox + maxMemSpinBox - + + + + &Minimum memory allocation: + + + minMemSpinBox + + + + The amount of memory available to store loaded Java classes. @@ -141,6 +141,16 @@ + + + + + + + maxMemSpinBox + + + diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 5da7f19f5..50039e879 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -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 @@ -437,6 +437,11 @@ void InstanceSettingsPage::on_javaTestBtn_clicked() checker->run(); } +void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i) +{ + updateThresholds(); +} + void InstanceSettingsPage::checkerFinished() { checker.reset(); @@ -447,3 +452,20 @@ 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(); + + if (maxMem >= sysMiB) { + ui->labelMaxMemIcon->setText(u8"✘"); + ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation exceeds your system memory capacity.")); + } else if (maxMem > (sysMiB * 0.9)) { + ui->labelMaxMemIcon->setText(u8"⚠"); + ui->labelMaxMemIcon->setToolTip(tr("Your maximum memory allocation approaches your system memory capacity.")); + } else { + ui->labelMaxMemIcon->setText(u8"✔"); + ui->labelMaxMemIcon->setToolTip(""); + } +} diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.h b/launcher/ui/pages/instance/InstanceSettingsPage.h index 97d1296fe..7450188d5 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.h +++ b/launcher/ui/pages/instance/InstanceSettingsPage.h @@ -77,10 +77,13 @@ public: 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(); diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.ui b/launcher/ui/pages/instance/InstanceSettingsPage.ui index 8b3c33702..43488aa2c 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.ui +++ b/launcher/ui/pages/instance/InstanceSettingsPage.ui @@ -112,59 +112,29 @@ false - - - + + + - Minimum memory allocation: + PermGen: - - - - The maximum amount of memory Minecraft is allowed to use. - - - MiB - - - 128 - - - 65536 - - - 128 - - - 1024 + + + + Maximum memory allocation: - - - - The amount of memory Minecraft is started with. - - - MiB - - - 128 - - - 65536 - - - 128 - - - 256 + + + + Note: Permgen is set automatically by Java 8 and later - + The amount of memory available to store loaded Java classes. @@ -186,24 +156,67 @@ - - - - PermGen: + + + + The maximum amount of memory Minecraft is allowed to use. + + + MiB + + + 128 + + + 65536 + + + 128 + + + 1024 - - + + - Maximum memory allocation: + Minimum memory allocation: - - + + + + The amount of memory Minecraft is started with. + + + MiB + + + 128 + + + 65536 + + + 128 + + + 256 + + + + + - Note: Permgen is set automatically by Java 8 and later + + + + Qt::AlignCenter + + + maxMemSpinBox