The hidden cost of mpsc channels
Recently I have been spending a lot of time analyzing and optimizing memory usage in our Rust reverse-proxy, agentgateway. One thing that repeatedly came up was a surprisingly large amount of memory allocated to innocent-looking Tokio mpsc channels. In my naive understanding, I would have assumed the following allocation pattern: struct BigStruct { data: [u8; 1024], } fn main() { // Allocates ~1024 bytes let _ = tokio::sync::mpsc::channel::(1); // Allocates ~1024*1024 bytes let _ = tokio::sync::mpsc::channel::(1024); } However, in practice both of these are wrong: they each allocate 32kb!
howardjohn's blog