Allow to limit max memory working set size

PR #16485.
This commit is contained in:
Vladimir Golovnev 2022-03-12 21:40:02 +03:00 committed by GitHub
parent ba147d72b9
commit 11c45db2ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 0 deletions

View file

@ -65,6 +65,7 @@
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrent.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/iconprovider.h"
#include "base/logger.h"
#include "base/net/downloadmanager.h"
@ -121,6 +122,9 @@ Application::Application(int &argc, char **argv)
, m_running(false)
, m_shutdownAct(ShutdownDialogAction::Exit)
, m_commandLineArgs(parseCommandLine(this->arguments()))
#ifdef Q_OS_WIN
, m_storeMemoryWorkingSetLimit(SETTINGS_KEY("MemoryWorkingSetLimit"))
#endif
, m_storeFileLoggerEnabled(FILELOGGER_SETTINGS_KEY("Enabled"))
, m_storeFileLoggerBackup(FILELOGGER_SETTINGS_KEY("Backup"))
, m_storeFileLoggerDeleteOld(FILELOGGER_SETTINGS_KEY("DeleteOld"))
@ -204,6 +208,22 @@ const QBtCommandLineParameters &Application::commandLineArgs() const
return m_commandLineArgs;
}
#ifdef Q_OS_WIN
int Application::memoryWorkingSetLimit() const
{
return m_storeMemoryWorkingSetLimit.get(512);
}
void Application::setMemoryWorkingSetLimit(const int size)
{
if (size == memoryWorkingSetLimit())
return;
m_storeMemoryWorkingSetLimit = size;
applyMemoryWorkingSetLimit();
}
#endif
bool Application::isFileLoggerEnabled() const
{
return m_storeFileLoggerEnabled.get(true);
@ -602,6 +622,10 @@ void Application::processParams(const QStringList &params)
int Application::exec(const QStringList &params)
{
#ifdef Q_OS_WIN
applyMemoryWorkingSetLimit();
#endif
Net::ProxyConfigurationManager::initInstance();
Net::DownloadManager::initInstance();
IconProvider::initInstance();
@ -771,6 +795,29 @@ void Application::shutdownCleanup(QSessionManager &manager)
}
#endif
#ifdef Q_OS_WIN
void Application::applyMemoryWorkingSetLimit()
{
const int UNIT_SIZE = 1024 * 1024; // MiB
const SIZE_T maxSize = memoryWorkingSetLimit() * UNIT_SIZE;
const SIZE_T minSize = std::min<SIZE_T>((64 * UNIT_SIZE), (maxSize / 2));
if (!::SetProcessWorkingSetSizeEx(::GetCurrentProcess(), minSize, maxSize, QUOTA_LIMITS_HARDWS_MAX_ENABLE))
{
const DWORD errorCode = ::GetLastError();
QString message;
LPVOID lpMsgBuf = nullptr;
if (::FormatMessageW((FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS)
, nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), reinterpret_cast<LPWSTR>(&lpMsgBuf), 0, nullptr))
{
message = QString::fromWCharArray(reinterpret_cast<LPWSTR>(lpMsgBuf)).trimmed();
::LocalFree(lpMsgBuf);
}
LogMsg(tr("Failed to set physical memory (RAM) usage limit. Error code: %1. Error message: \"%2\"")
.arg(QString::number(errorCode), message), Log::WARNING);
}
}
#endif
void Application::cleanup()
{
// cleanup() can be called multiple times during shutdown. We only need it once.

View file

@ -88,6 +88,11 @@ public:
const QBtCommandLineParameters &commandLineArgs() const;
#ifdef Q_OS_WIN
int memoryWorkingSetLimit() const;
void setMemoryWorkingSetLimit(int size);
#endif
// FileLogger properties
bool isFileLoggerEnabled() const;
void setFileLoggerEnabled(bool value);
@ -121,6 +126,9 @@ private slots:
#endif
private:
#ifdef Q_OS_WIN
void applyMemoryWorkingSetLimit();
#endif
void initializeTranslation();
void processParams(const QStringList &params);
void runExternalProgram(const BitTorrent::Torrent *torrent) const;
@ -146,6 +154,9 @@ private:
QTranslator m_translator;
QStringList m_paramsQueue;
#ifdef Q_OS_WIN
SettingValue<int> m_storeMemoryWorkingSetLimit;
#endif
SettingValue<bool> m_storeFileLoggerEnabled;
SettingValue<bool> m_storeFileLoggerBackup;
SettingValue<bool> m_storeFileLoggerDeleteOld;

View file

@ -64,6 +64,7 @@ namespace
RESUME_DATA_STORAGE,
#if defined(Q_OS_WIN)
OS_MEMORY_PRIORITY,
MEMORY_WORKING_SET_LIMIT,
#endif
// network interface
NETWORK_IFACE,
@ -196,6 +197,8 @@ void AdvancedSettings::saveAdvancedSettings()
break;
}
session->setOSMemoryPriority(prio);
static_cast<Application *>(QCoreApplication::instance())->setMemoryWorkingSetLimit(m_spinBoxMemoryWorkingSetLimit.value());
#endif
// Async IO threads
session->setAsyncIOThreads(m_spinBoxAsyncIOThreads.value());
@ -436,6 +439,15 @@ void AdvancedSettings::loadAdvancedSettings()
addRow(OS_MEMORY_PRIORITY, (tr("Process memory priority (Windows >= 8 only)")
+ ' ' + makeLink("https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-memory_priority_information", "(?)"))
, &m_comboBoxOSMemoryPriority);
m_spinBoxMemoryWorkingSetLimit.setMinimum(1);
m_spinBoxMemoryWorkingSetLimit.setMaximum(std::numeric_limits<int>::max());
m_spinBoxMemoryWorkingSetLimit.setSuffix(tr(" MiB"));
m_spinBoxMemoryWorkingSetLimit.setValue(static_cast<Application *>(QCoreApplication::instance())->memoryWorkingSetLimit());
addRow(MEMORY_WORKING_SET_LIMIT, (tr("Physical memory (RAM) usage limit")
+ ' ' + makeLink("https://wikipedia.org/wiki/Working_set", "(?)"))
, &m_spinBoxMemoryWorkingSetLimit);
#endif
// Async IO threads

View file

@ -82,6 +82,7 @@ private:
// OS dependent settings
#if defined(Q_OS_WIN)
QComboBox m_comboBoxOSMemoryPriority;
QSpinBox m_spinBoxMemoryWorkingSetLimit;
#endif
#ifndef Q_OS_MACOS