Skip to content

Truly independent web browser

MediaServer+VideoDecoderServer: Set a fixed mmap threshold for malloc

Zaggy1024956a8f8e9653
diff --git a/Libraries/LibMediaCodecs/CMakeLists.txt b/Libraries/LibMediaCodecs/CMakeLists.txt
index 24bcc60931a..71f9224508a 100644
--- a/Libraries/LibMediaCodecs/CMakeLists.txt
+++ b/Libraries/LibMediaCodecs/CMakeLists.txt
@@ -18,6 +18,7 @@ set(SOURCES
FFmpeg/FFmpegFunctions.cpp
FFmpeg/FFmpegHelpers.cpp
FFmpeg/FFmpegVideoDecoder.cpp
+ FFmpeg/MallocTuning.cpp
HardwareVideoDecoders.cpp
NonReferenceFrameDetector.cpp
VideoDecoderRegistry.cpp
diff --git a/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.cpp b/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.cpp
new file mode 100644
index 00000000000..f433f824ab8
--- /dev/null
+++ b/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.cpp
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/Format.h>
+#include <LibMediaCodecs/FFmpeg/MallocTuning.h>
+
+#if defined(__GLIBC__)
+# include <malloc.h>
+#endif
+
+namespace Media::FFmpeg {
+
+void set_fixed_mmap_threshold()
+{
+#if defined(__GLIBC__)
+ // FFmpeg, system and bundled alike, allocates through glibc, including CPU-decoded video frame buffers. When one
+ // of those buffers is freed, glibc normally raise its threshold past that size and serves the next ones from its
+ // arenas, which keeps them resident after the decoder is gone. Set a fixed threshold to keep them mapped
+ // separately, so they are unmapped when freed.
+ if (mallopt(M_MMAP_THRESHOLD, 128 * KiB) == 0)
+ warnln("Could not set the mmap threshold of malloc");
+#endif
+}
+
+}
diff --git a/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.h b/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.h
new file mode 100644
index 00000000000..0f6b329f6bc
--- /dev/null
+++ b/Libraries/LibMediaCodecs/FFmpeg/MallocTuning.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibMediaCodecs/Export.h>
+
+namespace Media::FFmpeg {
+
+MEDIACODECS_API void set_fixed_mmap_threshold();
+
+}
diff --git a/Services/MediaServer/main.cpp b/Services/MediaServer/main.cpp
index d835c04a9e7..fed3027c4eb 100644
--- a/Services/MediaServer/main.cpp
+++ b/Services/MediaServer/main.cpp
@@ -16,6 +16,7 @@
#include <LibMain/Main.h>
#include <LibMedia/DecoderRegistrations.h>
#include <LibMedia/RemoteVideoDecoder.h>
+#include <LibMediaCodecs/FFmpeg/MallocTuning.h>
#include <MediaServer/ConnectionFromClient.h>
#include <MediaServer/Sandbox.h>
@@ -27,6 +28,8 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
+ Media::FFmpeg::set_fixed_mmap_threshold();
+
int crash_report_fd = -1;
StringView mach_server_name;
bool wait_for_debugger = false;
diff --git a/Services/VideoDecoderServer/main.cpp b/Services/VideoDecoderServer/main.cpp
index 5c175af44fa..bbb004a4f67 100644
--- a/Services/VideoDecoderServer/main.cpp
+++ b/Services/VideoDecoderServer/main.cpp
@@ -16,6 +16,7 @@
#include <LibIPC/SingleServer.h>
#include <LibMain/Main.h>
#include <LibMediaCodecs/FFmpeg/FFmpegVideoDecoder.h>
+#include <LibMediaCodecs/FFmpeg/MallocTuning.h>
#include <LibMediaCodecs/HardwareVideoDecoders.h>
#include <LibMediaCodecs/VideoDecoderRegistry.h>
#include <LibMediaCodecs/Vulkan/VulkanVideoDevice.h>
@@ -46,6 +47,8 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
+ Media::FFmpeg::set_fixed_mmap_threshold();
+
int crash_report_fd = -1;
StringView stats_log_directory;
StringView mach_server_name;