Skip to content

Commit 7392f05

Browse files
committed
Use jemalloc as global allocator
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 2ed4521 commit 7392f05

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

Cargo.toml

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

1010
[dependencies]
11+
tikv-jemallocator = { version = "0.6", features = ["disable_initial_exec_tls"] }
12+
1113
bitcoin-payment-instructions = { version = "0.5.0", default-features = false, features = [
1214
"http",
1315
] }

src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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
6+
#[global_allocator]
7+
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
8+
39
use std::{
410
collections::{HashMap, HashSet},
511
convert::TryFrom,

0 commit comments

Comments
 (0)