Skip to content

Truly independent web browser

LibCore: Add a way to tell how short of memory the system is

Andreas Klingac93d7991a91
diff --git a/Libraries/LibCore/CMakeLists.txt b/Libraries/LibCore/CMakeLists.txt
index 42f4d405a27..c10e5199597 100644
--- a/Libraries/LibCore/CMakeLists.txt
+++ b/Libraries/LibCore/CMakeLists.txt
@@ -78,6 +78,15 @@ else()
list(APPEND SOURCES FileWatcherUnimplemented.cpp)
endif()
+# FIXME: Implement MemoryPressure for Windows and *BSD
+if (LINUX)
+ list(APPEND SOURCES Platform/MemoryPressureLinux.cpp)
+elseif (APPLE AND NOT IOS)
+ list(APPEND SOURCES Platform/MemoryPressureMacOS.cpp)
+else ()
+ list(APPEND SOURCES Platform/MemoryPressureUnimplemented.cpp)
+endif()
+
# FIXME: Implement ProcessStatistics for Windows and *BSD
if (LINUX)
list(APPEND SOURCES Platform/ProcessStatisticsLinux.cpp)
diff --git a/Libraries/LibCore/Platform/MemoryPressure.h b/Libraries/LibCore/Platform/MemoryPressure.h
new file mode 100644
index 00000000000..1c20454a586
--- /dev/null
+++ b/Libraries/LibCore/Platform/MemoryPressure.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Types.h>
+#include <LibCore/Export.h>
+
+namespace Core::Platform {
+
+// How short of memory the system as a whole is, as far as the operating system lets us tell.
+enum class MemoryPressure : u8 {
+ Normal,
+ // The system is short of memory, and has started to compress or swap it out.
+ Warning,
+ // The system is about to run out of memory, and will soon end processes to get some back.
+ Critical,
+};
+
+// NB: This is cheap enough to call every few seconds. Where the system can't tell, the pressure is always normal.
+CORE_API MemoryPressure system_memory_pressure();
+
+}
diff --git a/Libraries/LibCore/Platform/MemoryPressureLinux.cpp b/Libraries/LibCore/Platform/MemoryPressureLinux.cpp
new file mode 100644
index 00000000000..9644f4c6757
--- /dev/null
+++ b/Libraries/LibCore/Platform/MemoryPressureLinux.cpp
@@ -0,0 +1,55 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/CharacterTypes.h>
+#include <AK/GenericLexer.h>
+#include <AK/Optional.h>
+#include <LibCore/File.h>
+#include <LibCore/Platform/MemoryPressure.h>
+
+namespace Core::Platform {
+
+// Linux tells how much memory it could hand out without swapping, but not how short of it the system is. It is about
+// to run out once little of that is left: a twentieth of all memory, but at least 200 MiB, and on a machine with much
+// memory, no more than 1 GiB.
+static constexpr u64 critical_available_memory_percent = 5;
+static constexpr u64 minimum_critical_available_memory_in_kib = 200 * MiB / KiB;
+static constexpr u64 maximum_critical_available_memory_in_kib = 1 * GiB / KiB;
+
+MemoryPressure system_memory_pressure()
+{
+ auto file = Core::File::open("/proc/meminfo"sv, Core::File::OpenMode::Read);
+ if (file.is_error())
+ return MemoryPressure::Normal;
+ auto contents = file.value()->read_until_eof();
+ if (contents.is_error())
+ return MemoryPressure::Normal;
+
+ Optional<u64> total_in_kib;
+ Optional<u64> available_in_kib;
+ for (auto line : StringView { contents.value() }.lines()) {
+ GenericLexer lexer { line };
+ auto name = lexer.consume_until(':');
+ lexer.consume_specific(':');
+ lexer.ignore_while(is_ascii_space);
+ auto value = lexer.consume_while(is_ascii_digit).to_number<u64>();
+ if (name == "MemTotal"sv)
+ total_in_kib = value;
+ else if (name == "MemAvailable"sv)
+ available_in_kib = value;
+ }
+
+ // NB: Kernels older than 3.14 don't report the available memory.
+ if (!total_in_kib.has_value() || !available_in_kib.has_value() || *total_in_kib == 0)
+ return MemoryPressure::Normal;
+
+ auto critical_available_memory_in_kib = clamp(*total_in_kib / 100 * critical_available_memory_percent, minimum_critical_available_memory_in_kib, maximum_critical_available_memory_in_kib);
+ if (*available_in_kib <= critical_available_memory_in_kib)
+ return MemoryPressure::Critical;
+ return MemoryPressure::Normal;
+}
+
+}
diff --git a/Libraries/LibCore/Platform/MemoryPressureMacOS.cpp b/Libraries/LibCore/Platform/MemoryPressureMacOS.cpp
new file mode 100644
index 00000000000..2e0afe57da7
--- /dev/null
+++ b/Libraries/LibCore/Platform/MemoryPressureMacOS.cpp
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Platform.h>
+
+#if !defined(AK_OS_MACOS)
+# error "This file is only available on macOS"
+#endif
+
+#include <LibCore/Platform/MemoryPressure.h>
+#include <dispatch/dispatch.h>
+#include <sys/sysctl.h>
+
+namespace Core::Platform {
+
+MemoryPressure system_memory_pressure()
+{
+ // NB: The kernel reports the same levels that a dispatch source of the memory pressure type delivers.
+ int level = 0;
+ size_t size = sizeof(level);
+ if (sysctlbyname("kern.memorystatus_vm_pressure_level", &level, &size, nullptr, 0) < 0)
+ return MemoryPressure::Normal;
+
+ switch (level) {
+ case DISPATCH_MEMORYPRESSURE_WARN:
+ return MemoryPressure::Warning;
+ case DISPATCH_MEMORYPRESSURE_CRITICAL:
+ return MemoryPressure::Critical;
+ default:
+ return MemoryPressure::Normal;
+ }
+}
+
+}
diff --git a/Libraries/LibCore/Platform/MemoryPressureUnimplemented.cpp b/Libraries/LibCore/Platform/MemoryPressureUnimplemented.cpp
new file mode 100644
index 00000000000..e532b84ece9
--- /dev/null
+++ b/Libraries/LibCore/Platform/MemoryPressureUnimplemented.cpp
@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibCore/Platform/MemoryPressure.h>
+
+namespace Core::Platform {
+
+MemoryPressure system_memory_pressure()
+{
+ return MemoryPressure::Normal;
+}
+
+}