Just as the title suggests, I found out there's a memory leak when consumers process items slowly.
While the channel's queue will eventually be drained by any given number of consumers, the internal VecDeque 's excess capacity will never be freed (because that's how most rust's collections work, they over-allocate in advance to prevent frequent allocations).
This is notorious when you use flume in long-lived tasks/threads: fluctuations (usually network/db) sometimes cause consumers to process items slowly, causing the VecDeque to allocate extra space.
PoC:
use std::{hint::black_box, thread::park, time::Duration};
use flume;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
let (tx, rx) = flume::unbounded();
let mut cont: u64 = 0;
tokio::spawn(async move {
while let Ok(_val) = rx.try_recv() {
if cont % 1000 == 0 {
println!("Capacity: {}", rx.len());
sleep(Duration::from_millis(1)).await;
// rx.shrink_to_fit();
}
cont += 1;
}
println!("cont: {cont:?}");
});
for _ in 0..1000000u64 {
let waste = black_box([10u8; 100]);
tx.try_send(waste).ok();
}
park();
}
Note the commented line: there's a method that basically calls the inner queue's shrink_to_fit method to drop excess capacity.
I've submitted a PR with shrink_to_fit() and queue_capacity(), respectively :D
Just as the title suggests, I found out there's a memory leak when consumers process items slowly.
While the channel's queue will eventually be drained by any given number of consumers, the internal
VecDeque
's excess capacity will never be freed (because that's how most rust's collections work, they over-allocate in advance to prevent frequent allocations).This is notorious when you use flume in long-lived tasks/threads: fluctuations (usually network/db) sometimes cause consumers to process items slowly, causing the VecDeque to allocate extra space.
PoC:
Note the commented line: there's a method that basically calls the inner queue's
shrink_to_fit
method to drop excess capacity. I've submitted a PR withshrink_to_fit()
andqueue_capacity()
, respectively :D