Correctly releases the Vector memory.

Source: Internet
Author: User

When I was reading Weibo today, someone raised a question about Vector Memory leakage ).

The blogger used Vector to store some data, but found that the memory was not released after the clear () execution, so it was suspected that memory leakage had occurred. Then someone replied:

"The clear of vector does not affect capacity. You should swap an empty vector ."

I didn't know what the reviewer was talking about at the beginning, so I searched Google.vector swap clearSimilar problems have been found and some solutions have been provided.

This problem has already been pointed out in "clause 17" in "valid STL ".

After a large amount of data is inserted to a vector or string, even if a large amount of data is deleted or all are deleted, that is, clear) does not change the container capacity), the memory is still occupied. To avoid this situation, we should try to change the container capacity so that the shrink to fit required by the current data is as small as possible)

The solution provided by objective STL is:

 
 
  1. Vector <type> v;
  2. //... Add many elements to v
  3. //... Delete many elements in v here
  4. Vector <type> (v). swap (v );
  5. // At this time, the capacity of v is as much as possible to match the number of currently contained elements.
  6. // For a string, it may be as follows:
  7. String (s). swap (s );

That is, create a temporary copy that is consistent with the original vector. It is worth noting that the copy capacity is as small as possible to meet the required data. The copy is then exchanged with the original vector v. Now, after the SWAp is executed, the temporary variables will be destroyed and the memory will be released. At this time, v is the original temporary copy, and the temporary copy after switching is a vector with a very large capacity but has been destroyed)

To prove this, I wrote a program as follows:

 
 
  1. # Include <iostream>
  2. # Include <vector>
  3. Using namespace std;
  4. Vector <string> v;
  5. Char ch;
  6. Int main ()
  7. {
  8. For (int I = 0; I <1000000; I ++)
  9. V. push_back ("abcdefghijklmn ");
  10. Cin> ch;
  11. // Check whether the memory usage is 54 MB.
  12. V. clear ();
  13. Cin> ch;
  14. // Check again, still occupying 54 M
  15. Cout <"Vector capacity:" <v. capacity () <endl;
  16. // The capacity is 1048576
  17. Vector <string> (v). swap (v );
  18. Cout <"Vector capacity:" <v. capacity () <endl;
  19. // The capacity is 0 at this time
  20. Cin> ch;
  21. // Check the memory. If 10 M + is released, the data memory is used.
  22. Return 0;
  23. }
Summary

From this case, I know little about STL and usually know how to use the clear method to clear the vector.

On the other hand, I also have some knowledge about the design idea of STL. After creating a vector, the actual capacity of the vector is generally larger than that of the given data. This should be done to avoid too much memory reallocation.

Of course, although the above method releases the memory, it also increases the time consumption for copying data. However, when you need to re-adjust the capacity, the number of elements in the vector itself is small, so the time consumption is negligible.

Therefore, we recommend that you callclear()Changeswap()Right.

Http://blog.jobbole.com/37700/

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.