Memory release of Vector

Source: Internet
Author: User

Recently I ran a program, and the result was 2 GB of memory used up, and the result was not calculated. std: bad_alloc appeared. It should be because the memory space in some part of the program was not released in time and the program was checked. It is found that vector is used in many places, and some of them will be very large and only contain intermediate results. It is only because it needs to be accessed in multiple places and is defined as a member variable of the class, the space will not be released until this class is parsed. It seems that the memory should be released after the intermediate function of this class is completed in the program. So I began to explore how to release the vector memory.

1. clear (), a member function of vector ()

Clear () only clears the variables in the vector and does not release the memory. A simple test procedure is as follows,

int main(){
  vector<double> a;
  for(unsigned int i=0;i<10;i++){
  a.push_back(i+i/10);
  }
  cout << a.size() << " " << a.capacity() << endl;
  a.clear(); cout << a.size() << " " << a.capacity() << endl;  return 0;
}

The output is:

10 16

0 16

Size indicates the number of elements in the vector, and capacity indicates the memory occupied by the vector. The unit is the memory occupied by the element type in vectot.

We can see that the memory space requested by the vector is not equal to the number of elements, and the memory space of the vector can be dynamically increased, for example, if vector <double> a applies for only four double spaces at the beginning, when the number of elements exceeds four, the vector will re-allocate the memory. When there are 10 elements, the vector occupies 16 double spaces. After clear is executed, the number of elements is 0, but the occupied memory space is not changed. That is to say, the memory space occupied by vector <double> a is not released. Because it is not the space applied for by new, delete cannot be used. How can we release such memory?

2. swap Method

In the http://topic.csdn.net/u/20091202/15/817018d4-e0fc-4229-94b7-0869c9366a53.html provides two ways to release the memory occupied by the vector, try it out.

a.swap(vector<double>());

An error is reported during compilation. error: no matching function for call to 'std: vector <double, std: allocator <double >:: swap (std :: vector <double, std: allocator <double>) '/usr/include/c ++/4.4/bits/stl_vector.h: 929: note: candidates are: void std :: vector <_ Tp, _ Alloc >:: swap (std: vector <_ Tp, _ Alloc> &) [with _ Tp = double, _ Alloc = std :: allocator <double>]

Swap parameters should be referenced by a vector. Although swap exchanges the space of vector <double> a with a temporary variable, after switching, the space of a is 0, the space of the temporary variable is the space before a, and then the memory space is released when the process ends. In this way, the space of a is released when a is not a temporary variable. Change the preceding statement to two,

vector<double> b;
a.swap(b);

In this way, no error is reported. This is the difference between reference and value transfer.

The article also provides a method. After a try, the occupied space has not changed and I don't know why. The Link provides some content for reference. Http://oss.org.cn /? Action-viewnews-itemid-3744

3. Memory released when array elements are pointers

In many cases, we use a vector to save a set of pointers,

vector<classType*> S;

{
classType *item = new classType();
S.push_back(item);
}

At this time, swap only clears the space occupied by these pointers, but the space occupied by the instance pointing to the class is not released. For this vector, You need to manually release the space.

ClassType * item = NULL;
For (I = 0; I <S. size (); I ++ ){
Item = S [I]; // release the space pointed to by the vector pointer Element
Delete (classType *) item;
} Vector <classType *> Stmp; // release space occupied by the vector S. swap (Stmp );

In a function, the Temporary Variable releases the memory when the function exits. If you want to release the memory as soon as possible, you can use new to apply for the space of the temporary variable and then delete it as soon as possible. As follows,

vector<classType*> *Stemp = new vector<classType*>();
S.swap(*Stmp);
for(i=0;i<Stmp->size();i++){
delete (clasType*) (*Stmp)[i];
}
delete vector<classType*> Stmp;

The third part is just an idea and has not been verified yet.

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.