zoukankan      html  css  js  c++  java
  • C++ 如何快速清空vector以及释放vector内存?

    平时我们在写代码时候,有思考过要主动去释放vector的内存吗?

    1、对于数据量不大的vector,没有必要自己主动释放vector,一切都交给操作系统。

    2、但是对于大量数据的vector,在vector里面的数据被删除后,主动去释放vector的内存就变得很有必要了!

    读者可以新建一个控制台程序,把代码运行起来看输出,且看代码:

    [cpp] view plain copy
     
    1. #include <iostream>  
    2. #include <vector>  
    3. #include <string>  
    4. #include <Windows.h>  
    5. #include <Psapi.h>  
    6. #pragma comment(lib, "Psapi.lib")  
    7.   
    8. using namespace std;  
    9.   
    10. //GetCurPorcessMemory  
    11. bool GetCurProcessMemory(HANDLE handle, std::wstring& workingSize, std::wstring& peakWorkingSize)  
    12. {  
    13.     //HANDLE handle = GetCurrentProcess();  
    14.     PROCESS_MEMORY_COUNTERS pmc;  
    15.     if (GetProcessMemoryInfo(handle, &pmc, sizeof(pmc)))  
    16.     {  
    17.         int size = pmc.WorkingSetSize/1024;  
    18.         wchar_t buf[10] = {0};  
    19.         _ltow(size, buf, 10);  
    20.         workingSize = std::wstring(buf);  
    21.   
    22.         size = pmc.PeakWorkingSetSize/1024;  
    23.         _ltow(size, buf, 10);  
    24.         peakWorkingSize = std::wstring(buf);  
    25.   
    26.         return true;  
    27.     }  
    28.     return false;  
    29. }  
    30.   
    31. int _tmain(int argc, _TCHAR* argv[])  
    32. {  
    33.     std::wstring wszWorking, wszPeakWorking;  
    34.     vector<string> ary;  
    35.   
    36.     for (int i=0; i<1000000; i++)  
    37.     {  
    38.         ary.push_back("hello vector");  
    39.     }  
    40.   
    41.     wchar_t wch;  
    42.     wcin >> wch;  
    43.   
    44.     GetCurProcessMemory(GetCurrentProcess(), wszWorking, wszPeakWorking);// 此时检查内存情况  
    45.     wcout << "Working : " << wszWorking.c_str() << " PeakWorking : " << wszPeakWorking.c_str() << endl;  
    46.   
    47.     wcin >> wch;  
    48.   
    49.     //  
    50.     ary.clear();  
    51.     wcout << "vector clear" << endl;  
    52.     wcout << "vector capacity " << ary.capacity() << endl;      
    53.     GetCurProcessMemory(GetCurrentProcess(), wszWorking, wszPeakWorking);// 此时再次检查  
    54.     wcout << "Working : " << wszWorking.c_str() << " PeakWorking : " << wszPeakWorking.c_str() << endl;  
    55.   
    56.     wcin >> wch;  
    57.     //vector<string>(ary).swap(ary);  
    58.     ary.swap(vector<string>(ary));      
    59.     wcout << "vector swap" << endl;  
    60.     wcout << "vector capacity " << ary.capacity() << endl;// 此时容量为0     
    61.     GetCurProcessMemory(GetCurrentProcess(), wszWorking, wszPeakWorking);// 检查内存  
    62.     wcout << "Working : " << wszWorking.c_str() << " PeakWorking : " << wszPeakWorking.c_str() << endl;  
    63.   
    64.     wcout << "vector size : " << ary.size() << endl;//0  
    65.   
    66.     //getchar();  
    67.     system("pause");  
    68.   
    69.     return 0;  
    70. }  

    https://blog.csdn.net/hellokandy/article/details/78500067

  • 相关阅读:
    【转】做好测试计划和测试用例工作的关键
    【转】RESTful Web Services初探
    最快排序和搜索算法的最简代码实现_转
    排序算法
    libevent简述
    linux异步IO--aio
    长志气戒傲气 必须时刻保持冷静
    LACP-链路聚合
    AM335x移植linux内核_转
    4种用于构建嵌入式linux系统的工具_转
  • 原文地址:https://www.cnblogs.com/findumars/p/8732317.html
Copyright © 2011-2022 走看看