Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak in unbounded channels #146

Open
rakbladsvalsen opened this issue May 3, 2024 · 1 comment
Open

Memory leak in unbounded channels #146

rakbladsvalsen opened this issue May 3, 2024 · 1 comment

Comments

@rakbladsvalsen
Copy link

rakbladsvalsen commented May 3, 2024

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

@rakbladsvalsen
Copy link
Author

@zesterer I submitted a new PR a few weeks ago. This one should be way easier to review: #148

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant