When working with unbounded_channel, I’ve observed that memory consumption increases over time but does not decrease. My question is: does the implementation intentionally retain allocated memory for reuse, or should memory be released after items are consumed?
From the name unbounded_channel I assume you mean tokio's unbounded channel, right? Based on the docs I'd think that tokio does drop empty blocks from the channel's message queue:
When all values in a block have been received, it becomes empty. It will then be freed, unless the channel’s first block (where newly-sent elements are being stored) has no next block. In that case, the empty block is reused as the next block.
But maybe your allocator doesn't return the freed memory back to your OS but keeps it cached for future allocations? That's just guesswork on my part though.
Thanks for the reply. Yes, I’m thinking along the same lines. In my case, the sender is producing messages faster than the consumer can process them (due to awaiting Kafka publishes), and message sending is continuous. As a result, the first block in the channel is never fully emptied. Blocks are therefore retained for reuse instead of being freed, which explains why memory usage can appear to remain high even after some messages have been consumed.
I also tried a different version where the receiver work is offloaded to a spawned task, and memory usage looks much bette, likely because the receiver can now keep up with the producer.
I am still doing some trial to confirm this though
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.