提高ASIO流缓冲调用消耗后不释放内存? [英] boost asio streambuf don't release memory after calling consume?
问题描述
boost::asio::streambuf b;
...
void handler(const boost::system::error_code& e, std::size_t size)
{
if (!e)
{
std::stringstream sstr(std::string((std::istreambuf_iterator<char>(&b)),
std::istreambuf_iterator<char>()));
b.consume(size);
...
}
}
...
boost::asio::async_read_until(s, b, "END\r\n", handler);
在消耗
方法被调用时,由流缓冲占用b
内存不会被释放。内存将成长为 async_read_until
被多次调用。是我的使用是否正确?有什么办法释放之前内存streambuf的get指针
?
when the consume
method is called, the memory occupied by streambuf b
is not released. The memory will grow up as async_read_until
is called multiple times. Is my usage correct? Is there any way to free the memory before the get pointer
of streambuf?
推荐答案
ASIO ::流缓冲基于的std ::载体,生长需要,但从来没有收缩。
因此,消费()是不应该释放内存,它只是调整内部指针:
asio::streambuf is based on std::vector that grows as needed, but never shrinks. So, consume() is not supposed to release memory, it just adjusts internal pointers:
void consume(std::size_t n)
{
if (egptr() < pptr())
setg(&buffer_[0], gptr(), pptr());
if (gptr() + n > pptr())
n = pptr() - gptr();
gbump(static_cast<int>(n));
}
但每次消费(),并再次阅读()的时候,内部缓冲区(矢量)被再利用,所以你不需要任何发布
But each time you consume() and read() again, the internal buffer (vector) is reused, so you don't need to release anything.
这篇关于提高ASIO流缓冲调用消耗后不释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!