|
-
July 1st, 2011, 08:14 PM
#1
Free memory from a long vector
What I wanted to do is to release the memory used by a vector (say vector<vector<int>>) and I used the swap trick, i.e., v.swap(vector<vector<int>>()).
However, what I observed that the swap trick work well for short vectors but NOT for long ones, for example I tried one vector of length 10,000,000, which took 1.3G in memory, after the swap there is still 1.0G not released.
I learned that this has something to do with the C++ runtime library, my question is that there a way to force the memory to be released by the process? I want to make one process release its memory so that another process can use it.
Below is the code I used for testing.
Thanks advance for any help
Code:
#include <iostream>
#include <vector>
using namespace std;
typedef unsigned long long int64;
int main()
{
{
vector<vector<int64>> batch;
{
vector<int64> v;
for (size_t i = 0; i < 12; ++i)
v.push_back(8000000000);
for (size_t i = 0; i < 10000000; ++i)
batch.push_back(v);
}
cout << "pause 1" << endl;
cin.ignore();
{
vector<vector<int64>> tmp;
batch.swap(tmp);
// I tried several things here.
//tmp.swap(batch);
//batch.clear();
//batch.shrink_to_fit();
//batch = tmp;
}
cout << "pause 2" << endl;
cin.ignore();
}
cout << "pause 3" << endl;
cin.ignore();
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|