boost::asio::streambuf 的大小将继续增加,直到调用 consume()。
即使调用了consume(),底层缓冲区使用的内存也永远不会被释放。
例如:下面的代码首先创建了一个streambuf,没有指定max_size。然后它将 14Mb 数据转储到 streambuf 中。然后它会消耗所有这些 14MB 数据。在 2000 点,streambuf.size() 为 0,但“top”显示该进程仍占用 14MB 内存。
我不想指定 max_size。无论如何在它为空后缩小streambuf?
#include <boost/asio.hpp>
#include <iostream>
#include <string>
int main()
{
{
boost::asio::streambuf b;
std::ostream os(&b);
for(int i= 0; i<1000000; ++i)
{
os << "Hello, World!\n";
}
std::cout<<"point 1000"<<std::endl;
std::cout<<"size="<<b.size()<<std::endl;
// at this point, the streambuf's size is close to 14MB.
b.consume(b.size());
std::cout<<"point 2000"<<std::endl;
std::cout<<"size="<<b.size()<<std::endl;
// at this point, the streambuf.size() is 0
// but the streambuf's underlying buffer, which is a vector I assume,
// still hold that 14MB space.
// "top" shows the process size is 14M
}
// at this point, "top" showed the process size shrinks to almost zero
std::cout<<"point 4000"<<std::endl;
return 0;
}