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 clear
Similar 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:
- Vector <type> v;
- //... Add many elements to v
- //... Delete many elements in v here
- Vector <type> (v). swap (v );
- // At this time, the capacity of v is as much as possible to match the number of currently contained elements.
- // For a string, it may be as follows:
- 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:
- # Include <iostream>
- # Include <vector>
- Using namespace std;
- Vector <string> v;
- Char ch;
- Int main ()
- {
- For (int I = 0; I <1000000; I ++)
- V. push_back ("abcdefghijklmn ");
- Cin> ch;
- // Check whether the memory usage is 54 MB.
- V. clear ();
- Cin> ch;
- // Check again, still occupying 54 M
- Cout <"Vector capacity:" <v. capacity () <endl;
- // The capacity is 1048576
- Vector <string> (v). swap (v );
- Cout <"Vector capacity:" <v. capacity () <endl;
- // The capacity is 0 at this time
- Cin> ch;
- // Check the memory. If 10 M + is released, the data memory is used.
- Return 0;
- }
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/