diff --git a/Cargo.lock b/Cargo.lock index 662b1f8ee..b48888766 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3225,6 +3225,12 @@ dependencies = [ "windows-link", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -3396,6 +3402,7 @@ dependencies = [ "stats_alloc", "tempfile", "thiserror 2.0.18", + "tikv-jemalloc-ctl", "tikv-jemallocator", "tokio", "tokio-rustls", @@ -5284,6 +5291,17 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "tikv-jemalloc-ctl" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "661f1f6a57b3a36dc9174a2c10f19513b4866816e13425d3e418b11cc37bc24c" +dependencies = [ + "libc", + "paste", + "tikv-jemalloc-sys", +] + [[package]] name = "tikv-jemalloc-sys" version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7" diff --git a/pgdog/Cargo.toml b/pgdog/Cargo.toml index 725aa94f3..20bc624e2 100644 --- a/pgdog/Cargo.toml +++ b/pgdog/Cargo.toml @@ -92,6 +92,7 @@ libc = "0.2" [target.'cfg(not(target_env = "msvc"))'.dependencies] tikv-jemallocator = "0.6" +tikv-jemalloc-ctl = "0.6" [build-dependencies] diff --git a/pgdog/src/lib.rs b/pgdog/src/lib.rs index 3e02cf07f..c1011ac13 100644 --- a/pgdog/src/lib.rs +++ b/pgdog/src/lib.rs @@ -52,6 +52,17 @@ static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[global_allocator] static GLOBAL: &stats_alloc::StatsAlloc = &stats_alloc::INSTRUMENTED_SYSTEM; +/// Enable jemalloc's background purge threads so freed memory is returned to +/// the OS after allocation bursts. +#[cfg(all(not(test), not(target_env = "msvc")))] +pub fn enable_jemalloc_background_thread() { + let _ = tikv_jemalloc_ctl::background_thread::write(true); +} + +/// No-op fallback where jemalloc is not the active allocator. +#[cfg(any(test, target_env = "msvc"))] +pub fn enable_jemalloc_background_thread() {} + /// Filter that dynamically installs or removes an inner /// [`TracingRateLimitLayer`] at runtime. /// diff --git a/pgdog/src/main.rs b/pgdog/src/main.rs index 684c87e55..e185551d2 100644 --- a/pgdog/src/main.rs +++ b/pgdog/src/main.rs @@ -19,6 +19,8 @@ use tokio::runtime::Builder; use tracing::{error, info, warn}; fn main() -> Result<(), Box> { + pgdog::enable_jemalloc_background_thread(); + let args = cli::Cli::parse(); let command = args.command.clone(); let mut overrides = pgdog::config::Overrides::default();