vector、map 內存釋放

1、vector測試

void TestVector()
{
	cout << "begin create vector" << endl;
	int iSize = 10000000;
	vector<int> test_vec;
	for (int i = 0; i < iSize; i++)
	{
		test_vec.push_back(i);
	}
	cout << "create vector end" << endl;

	Sleep(2*1000);
	cout << "clear vector" << endl;
	test_vec.clear();
	cout << "clear vector end" << endl;

	Sleep(1*1000);

	cout << "after clear new data come if the size bigger" << endl;
	for (int i = 0; i < iSize; i++)
	{
		test_vec.push_back(i);
	}
	cout << "new data push_back end" << endl;

	Sleep(1*1000);

	cout << "swap vector" << endl;

	{
		vector<int> tmp_vec;
		test_vec.swap(tmp_vec);
	}
	cout << "swap vector end" << endl;

	Sleep(1*1000);
}

測試過程:spa

 

 

 

 

結論:code

  1. vector clear 以後,內存沒有釋放,但在下次分配時會複用該塊內存。blog

  2. vector swap 空數據以後,會釋放內存。內存

 

 2、mapget

void TestMap()
{
	cout << "begin create map" << endl;
	int iSize = 1000000;
	map<int, int> test_map;
	for (int i = 0; i < iSize; i++)
	{
		test_map[i] = i;
	}
	cout << "create map end" << endl;

	Sleep(2*1000);
	
	cout << "clear map" << endl;
	test_map.clear();
	cout << "clear map end" << endl;

	getchar();

	cout << "swap map" << endl;

	{
		map<int, int> tmp_map;
		test_map.swap(tmp_map);
	}
	cout << "swap map end" << endl;
	getchar();
}

測試過程:class

 

 

 

結論:test

  1. map clear 以後,會釋放內存。map

  2. map swap 空數據以後,會釋放內存。im

相關文章
相關標籤/搜索