How does C + + quickly empty vectors and release vector memory?

Source: Internet
Author: User

Usually when we write code, have you thought about the initiative to release the vector memory?

1, for the small amount of data vector, there is no need to voluntarily release the vector, everything to the operating system.

2, but for a large number of data vector, the data inside the vector is deleted, the initiative to release the vector memory becomes very necessary!

Readers can create a new console program, run the code to see the output, and look at the code:

[CPP]View PlainCopy
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <Windows.h>
  5. #include <Psapi.h>
  6. #pragma comment (lib, "Psapi.lib")
  7. Using namespace std;
  8. Getcurporcessmemory
  9. BOOL Getcurprocessmemory (HANDLE HANDLE, std::wstring& workingsize, std::wstring& peakworkingsize)
  10. {
  11. //handle HANDLE = getcurrentprocess ();
  12. Process_memory_counters PMC;
  13. if (Getprocessmemoryinfo (handle, &PMC, sizeof (PMC)))
  14. {
  15. int size = PMC.  workingsetsize/1024;
  16. wchar_t buf[10] = {0};
  17. _ltow (Size, buf, 10);
  18. Workingsize = std::wstring (BUF);
  19. Size = PMC. peakworkingsetsize/1024;
  20. _ltow (Size, buf, 10);
  21. Peakworkingsize = std::wstring (BUF);
  22. return true;
  23. }
  24. return false;
  25. }
  26. int _tmain (int argc, _tchar* argv[])
  27. {
  28. Std::wstring wszworking, wszpeakworking;
  29. Vector<string> ary;
  30. For (int i=0; i<1000000; i++)
  31. {
  32. Ary.push_back ("Hello vector");
  33. }
  34. wchar_t WCH;
  35. Wcin >> WCh;
  36. Getcurprocessmemory (GetCurrentProcess (), wszworking, wszpeakworking); //Check memory condition at this time
  37. Wcout << "working:" << wszworking.c_str () << "peakworking:" << wszpeakworking.c_str ()  << Endl;
  38. Wcin >> WCh;
  39. //  
  40. Ary.clear ();
  41. Wcout << "vector clear" << Endl;
  42. Wcout << "vector capacity" << ary.capacity () << Endl;
  43. Getcurprocessmemory (GetCurrentProcess (), wszworking, wszpeakworking); //Check again at this time
  44. Wcout << "working:" << wszworking.c_str () << "peakworking:" << wszpeakworking.c_str ()  << Endl;
  45. Wcin >> WCh;
  46. //vector<string> (ary). Swap (ARY);
  47. Ary.swap (vector<string> (ary));
  48. Wcout << "vector swap" << Endl;
  49. Wcout << "vector capacity" << ary.capacity () << Endl; At this time the capacity is 0
  50. Getcurprocessmemory (GetCurrentProcess (), wszworking, wszpeakworking); //Check memory
  51. Wcout << "working:" << wszworking.c_str () << "peakworking:" << wszpeakworking.c_str ()  << Endl;
  52. Wcout << "vector size:" << ary.size () << Endl; 0
  53. //getchar ();
  54. System ("pause");
  55. return 0;
  56. }

78500067

How does C + + quickly empty vectors and release vector memory?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.