Skip to content

Commit 8491d7c

Browse files
authored
Use jemalloc as global allocator (#31)
Production lightning nodes were getting OOM-killed during boot. The bun process running the NAPI-RS native addon hit 857MB RSS in a 1GB container, leaving no headroom for channel monitor deserialization at startup. glibc's malloc fragments heavily under Rust's allocation patterns (many small, short-lived allocations interleaved with long-lived ones), inflating RSS 30-50% beyond actual usage. jemalloc's arena-based allocator and thread-local caching avoid this fragmentation. disable_initial_exec_tls is required because NAPI-RS addons are loaded via dlopen at runtime. Without it, jemalloc's TLS usage exhausts the static TLS block allocated at program start, since dlopen'd libraries get a smaller TLS allocation than statically linked ones.
1 parent 6f19d59 commit 8491d7c

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ version = "0.1.0"
77
[lib]
88
crate-type = ["cdylib"]
99

10+
[target.'cfg(unix)'.dependencies]
11+
tikv-jemallocator = { version = "0.6", features = ["disable_initial_exec_tls"] }
12+
1013
[dependencies]
1114
bitcoin-payment-instructions = { version = "0.5.0", default-features = false, features = [
1215
"http",

src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
#![deny(clippy::all)]
22

3+
/// Use jemalloc to reduce memory fragmentation during channel monitor
4+
/// deserialization and node boot. glibc malloc fragments heavily with
5+
/// Rust's alloc patterns (not available on Windows).
6+
#[cfg(unix)]
7+
#[global_allocator]
8+
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
9+
310
use std::{
411
collections::{HashMap, HashSet},
512
convert::TryFrom,

0 commit comments

Comments
 (0)