C++ STL容器类型的内存释放
C++ 的大部分STL容器类型不会随着生命周期的结束而自动释放内存,接下来将依次对其说明:1. vector 类型经测试,直到C++11的vector类型,用clear或者erase都无法释放内存,只有显示调用swap:std::vector<T>.swap(t_value);才能释放。vector析构时会释放内存空间。如下例:void testVectorMemory
·
C++ 的大部分STL容器类型不会随着生命周期的结束而自动释放内存,接下来将依次对其说明:
1. vector 类型
经测试,直到C++11的vector类型,用clear或者erase都无法释放内存,只有显示调用swap:
std::vector<T>.swap(t_value);
才能释放。
vector析构时会释放内存空间。
如下例:
void testVectorMemory()
{
std::string str;
str.append(1024, 'c');
sleep(5);
std::cout << "Before add vector: " << getVirtualMemory() << std::endl;
std::vector<std::string> testVec;
for (size_t i = 0; i<100000; i++)
{
testVec.push_back(str);
}
std::cout << "After add vector: " << getVirtualMemory() << std::endl;
sleep(5);
std::vector<std::string>::iterator iter = testVec.begin();
for (; iter!=testVec.end();)
{
iter = testVec.erase(iter);
}
std::cout << "After erase vector: " << getVirtualMemory() << std::endl;
sleep(5);
}
int main(int argc, char *argv[])
{
std::cout << "Before test: " << getVirtualMemory() << std::endl;
testVectorMemory();
sleep(10);
std::cout << "After test: " << getVirtualMemory() << std::endl;
sleep(100);
std::cout << "After test, after 100 seconds: " << getVirtualMemory() << std::endl;
return 0;
}
测试结果:
Before test: 4088
Before add vector: 4096
After add vector: 4620
After erase vector: 4624
After test: 4112
After test, after 100 seconds: 4116
2. map类型
经测试,map分配的空间无法被释放,无论是erase, swap,就算作为局部变量,析构后也不释放
测试代码:
void testMapMemory()
{
std::string str;
str.append(1024, 'c');
sleep(5);
std::cout << "Before add map: " << getVirtualMemory() << std::endl;
std::map<int, std::string> tempMap;
for (size_t i = 0; i<100000; i++)
{
tempMap[i] = str;
}
std::cout << "After add map: " << getVirtualMemory() << std::endl;
sleep(5);
std::map<int, std::string>::iterator iter = tempMap.begin();
for (; iter!=tempMap.end();)
{
iter = tempMap.erase(iter);
}
std::cout << "After erase map: " << getVirtualMemory() << std::endl;
sleep(5);
std::map<int, std::string>().swap(tempMap);
std::cout << "After swap map: " << getVirtualMemory() << std::endl;
sleep(5);
}
int main(int argc, char *argv[])
{
std::cout << "Before test: " << getVirtualMemory() << std::endl;
testMapMemory();
sleep(10);
std::cout << "After test: " << getVirtualMemory() << std::endl;
sleep(100);
std::cout << "After test, after 100 seconds: " << getVirtualMemory() << std::endl;
return 0;
}
测试结果:
Before test: 4096
Before add map: 4104
After add map: 7144
After erase map: 7148
After swap map: 7152
After test: 7156
After test, after 100 seconds: 7160
3. queue类型
经测试,pop方法只是删除数据,并未释放内存。swap方法或者析构只释放了部分内存。
代码:
void testQueueMemory()
{
std::string str;
str.append(1024, 'c');
sleep(5);
std::cout << "Before add queue: " << getVirtualMemory() << std::endl;
//. test basic type
std::queue<std::string> basicQueue;
for (size_t i = 0; i<100000; i++)
{
basicQueue.push(str);
}
std::cout << "After add queue: " << getVirtualMemory() << std::endl;
sleep(5);
while(!basicQueue.empty())
{
basicQueue.pop();
}
std::cout << "After pop queue: " << getVirtualMemory() << std::endl;
sleep(5);
std::queue<std::string>().swap(basicQueue);
std::cout << "After swap queue: " << getVirtualMemory() << std::endl;
sleep(5);
}
int main(int argc, char *argv[])
{
std::cout << "Before test: " << getVirtualMemory() << std::endl;
testQueueMemory();
sleep(10);
std::cout << "After test: " << getVirtualMemory() << std::endl;
sleep(100);
std::cout << "After test, after 100 seconds: " << getVirtualMemory() << std::endl;
return 0;
}
测试结果:
Before test: 4096
Before add queue: 4104
After add queue: 4504
After pop queue: 4508
After swap queue: 4232
After test: 4236
After test, after 100 seconds: 4240
4. deque类型
deque类型与queue类型唯一的区别是:
deque在使用pop后会释放一部分内存
代码:
void testDequeMemory()
{
std::string str;
str.append(1024, 'c');
sleep(5);
std::cout << "Before add queue: " << getVirtualMemory() << std::endl;
//. test basic type
std::deque<std::string> basicQueue;
for (size_t i = 0; i<100000; i++)
{
basicQueue.push_back(str);
}
std::cout << "After add queue: " << getVirtualMemory() << std::endl;
sleep(5);
while(!basicQueue.empty())
{
basicQueue.pop_back();
}
std::cout << "After pop queue: " << getVirtualMemory() << std::endl;
sleep(5);
}
int main(int argc, char *argv[])
{
std::cout << "Before test: " << getVirtualMemory() << std::endl;
testDequeMemory();
sleep(10);
std::cout << "After test: " << getVirtualMemory() << std::endl;
sleep(100);
std::cout << "After test, after 100 seconds: " << getVirtualMemory() << std::endl;
return 0;
}
测试结果:
Before test: 4096
Before add queue: 4104
After add queue: 4504
After pop queue: 4340
After test: 4232
After test, after 100 seconds: 4236
更多推荐
已为社区贡献1条内容
所有评论(0)