#7344·tokio

[docs] Codec Encoder and Decoder traits do not describe buffer management

Author: FirstyearCreated May 19, 2025Updated Sep 29, 2026
LabelsC-bugA-tokio-utilM-codec

Version All

Platform n/a

Description When implementing a codec, it's common to see the BytesMut struct grow to hundreds of MB that can't be reclaimed. This is a combination of issues, but the main is that there seems to be no way for a https://docs.rs/bytes/latest/bytes/struct.BytesMut.html#method.truncate to release memory from it's allocation.

The docs of BytesMut describe all the places where capacity is grown or preserved, but there is no mention of when capacity can be freed.

Nor does tokio-util describe how to ensure that the BytesMut doesn't continue to grow to excessive sizes during operation.

For example https://docs.rs/tokio-util/latest/tokio_util/codec/trait.Decoder.html#tymethod.decode says:

"An optimal buffer management strategy minimizes reallocations and over-allocations."

But gives no information on how to achieve this, nor does BytesMut clearly describe this.

As a result, this leaves developers confused how to manage memory (at best) and at worst, causes memory leaks of hundreds of MB (at worst).

Can this documentation about BytesMut management be improved?