33 #include <sys/resource.h>
35 #include <mach/mach.h>
47MemoryProfiler::LogCallback gLogCallback;
49MemoryProfiler::LogCallback getLogCallback() {
50 QMutexLocker locker(&gLogMutex);
57 QMutexLocker locker(&gLogMutex);
58 gLogCallback = std::move(callback);
61bool MemoryProfiler::isEnabled() {
68 PROCESS_MEMORY_COUNTERS_EX pmc;
69 if (GetProcessMemoryInfo(GetCurrentProcess(),
70 (PROCESS_MEMORY_COUNTERS*)&pmc,
72 return static_cast<long>(pmc.PeakWorkingSetSize / (1024 * 1024));
78 if (getrusage(RUSAGE_SELF, &usage) == 0) {
81 return usage.ru_maxrss / (1024 * 1024);
84 return usage.ru_maxrss / 1024;
94 PROCESS_MEMORY_COUNTERS_EX pmc;
95 if (GetProcessMemoryInfo(GetCurrentProcess(),
96 (PROCESS_MEMORY_COUNTERS*)&pmc,
98 return static_cast<long>(pmc.WorkingSetSize / (1024 * 1024));
101#elif defined(__APPLE__)
103 struct mach_task_basic_info info;
104 mach_msg_type_number_t size = MACH_TASK_BASIC_INFO_COUNT;
105 kern_return_t kerr = task_info(mach_task_self(),
106 MACH_TASK_BASIC_INFO,
109 if (kerr == KERN_SUCCESS) {
110 return static_cast<long>(info.resident_size / (1024 * 1024));
115 std::ifstream status(
"/proc/self/status");
117 while (std::getline(status, line)) {
118 if (line.substr(0, 6) ==
"VmRSS:") {
120 sscanf(line.c_str(),
"VmRSS: %ld kB", &rss_kb);
121 return rss_kb / 1024;
136 const LogCallback callback = getLogCallback();
137 if (currentMB >= 0 && peakMB >= 0) {
138 const QString message = QString(
"[MEM] %1: %2 MB (peak: %3 MB)")
145 qDebug().noquote() << message;
148 const QString message =
149 QString(
"[MEM] Failed to retrieve memory usage for: %1").arg(label);
153 qWarning().noquote() << message;
Application configuration and settings.
Cross-platform memory profiling utility.
static AppConfig & instance()
Get the singleton instance.
bool getMemoryProfilingEnabled() const
Whether memory profiling checkpoints are enabled (default: false).
static long getCurrentMemoryMB()
Get current actual memory usage in MB (current RSS).
static void checkpoint(const QString &label)
Log current memory usage with a label.
static long getPeakMemoryMB()
Get current peak memory usage in MB (maximum RSS since process start).
static void setLogCallback(LogCallback callback)
Set a callback for memory log messages (GUI hook).