boost :: asio :: streambuf的大小会持续增加,直到调用consume()。
即使在调用consume()之后,底层缓冲区使用的内存也不会被释放。
boost :: asio :: 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;
}
这与http://stackoverflow.com/questions/9552785/boost-asio-streambuf-dont-release-memory-after-calling-consume非常相似,但它并不表示您的问题的肯定答案。 – Steve