【 在 jackhu123 的大作中提到: 】
:
: 我也不是很懂,
: 在centos上验证了一下,std::vector可以直接“释放内存”(交还操作系统),std::map重现了
: ...................
验证代码如下:
#include <malloc.h>
class CTest {
public:
explicit CTest(void) {
std::cout << "constructor" << std::endl;
//m_pvctTest = new std::vector<int>();
for (int i = 0; i < 20000000; i++) {
//m_vctTest.push_back(i);
//m_pvctTest->push_back(i);
m_mapTest.insert(std::make_pair(i, i));
}
}
virtual ~CTest(void) {
std::cout << "destructor" << std::endl;
//if (m_pvctTest) {
// delete m_pvctTest;
// m_pvctTest = nullptr;
//}
malloc_trim(0);
}
protected:
//std::vector<int> m_vctTest;
//std::vector<int>* m_pvctTest;
std::map<int, int> m_mapTest;
};
void test_free_memory() {
std::cout << "start test" << std::endl;
{
CTest* pTest = new CTest;
delete pTest;
pTest = nullptr;
}
std::cout << "sleep 30 seconds" << std::endl;
sleep(30);
std::cout << "quit test" << std::endl;
}
"调试运行"程序,观察top,不能直接“释放内存”(交还操作系统)
"运行"程序,观察top,直接“释放内存”(交还操作系统)了
--
FROM 58.212.12.*