zoukankan      html  css  js  c++  java
  • vector、map 内存释放

    一、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);
    }

    测试过程:

    结论:

      1. vector clear 之后,内存没有释放,但在下次分配时会复用该块内存。

      2. vector swap 空数据之后,会释放内存。

     二、map

    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();
    }

    测试过程:

    结论:

      1. map clear 之后,会释放内存。

      2. map swap 空数据之后,会释放内存。

  • 相关阅读:
    External Interrupts in the x86 system. Part 1. Interrupt controller evolution
    虚拟机中断
    内核中断
    交换机三层转发vlanif
    centos 电池
    ironic port + host_id +device id
    arping
    2018-7-29-C#-强转会不会抛出异常
    2018-7-29-C#-强转会不会抛出异常
    2019-2-2-VisualStudio-扩展开发-添加菜单
  • 原文地址:https://www.cnblogs.com/SZxiaochun/p/8432817.html
Copyright © 2011-2022 走看看