Mpsc Channel reclaim memory

I am having trouble finding information on this however if I consume all messages on a mpsc channel, is it supposed to "reclaim/free" my memory?

Dropping all senders and recievers will free the memory

How should we handle memory in programs that run as daemons for a long time with senders and receivers that last the life of the program?

What are you expecting to happen? Keeping those senders and receivers around should keep the memory they share alive.

I was thinking once all receivers have received a message that the message would be dropped after that message had been processed. Otherwise long running channels would just leak memory, wouldn't they?

But how would the senders know that all recievers are dead? If you free the memory too early, a sender may try to access freed memory.

Since there is only one consumer, it seems possible to reuse memory from already read values, no?
It wouldn't reclaim memory be not allocate as much.

once a message is received, you'd expect the queue slot to be reused, as there is no need to keep it around

the original question is a little bit ambiguous, what is meant, returning the memory to the queue, to the heap, to the OS ?
i think the question is in regard to the asynchronous channels whose buffers can grow infinitely (growing capacity on the heap): whether this capacity is ever reduced again, or it will always stay at the worst-case size?

there is nothing about this in the documentation

looking at the source code, it becomes clear that the queue is implemented as a linked list, and that messages that are popped from the queue are freed immediately—so i guess this makes the answer "yes"

2 Likes

i think the question is in regard to the asynchronous channels whose buffers can grow infinitely

Yes, that is what I was thinking of in regard to this. Sorry if that was confusing. I've only used async unbounded mpsc channels, so I didn't even think about sync ones or how they would work.

looking at the source code , it becomes clear that the queue is implemented as a linked list, and that messages that are popped from the queue are freed immediately—so i guess this makes the answer "yes"

Thanks for that clarification!