K
kayjean
Hi. I have a small test.cc
I found that vector.clear() doesn't free the memory.
How to free the memory I used ??
thankx
//test.cc
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<unsigned long> List ;
struct Element {
List element_member;
void free() { element_member.clear(); };
};
typedef vector<Element> ElementMatrix ;
typedef vector<Element>::iterator ElementMatrixIterator ;
int main()
{
{
ElementMatrix A;
for(int i = 0 ; i < 500000 ; i++ )
{
Element B ;
B.element_member.resize (20) ;
for (unsigned long j = 0 ; j < 20 ; j++)
B.element_member[j] = j ;
A.push_back(B) ;
}
cerr << "phase 1 clear element" << endl;
for( ElementMatrixIterator it = A.begin() ; it != A.end() ; it++ )
it->free();
sleep( 10 );
cerr << "phase 2 clear matrix" << endl;
A.clear();
sleep( 10 );
}
cerr << "phase 3 free matrix" << endl;
sleep( 10 );
return 0;
}
Hao Kuang , Ku
I found that vector.clear() doesn't free the memory.
How to free the memory I used ??
thankx
//test.cc
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
typedef vector<unsigned long> List ;
struct Element {
List element_member;
void free() { element_member.clear(); };
};
typedef vector<Element> ElementMatrix ;
typedef vector<Element>::iterator ElementMatrixIterator ;
int main()
{
{
ElementMatrix A;
for(int i = 0 ; i < 500000 ; i++ )
{
Element B ;
B.element_member.resize (20) ;
for (unsigned long j = 0 ; j < 20 ; j++)
B.element_member[j] = j ;
A.push_back(B) ;
}
cerr << "phase 1 clear element" << endl;
for( ElementMatrixIterator it = A.begin() ; it != A.end() ; it++ )
it->free();
sleep( 10 );
cerr << "phase 2 clear matrix" << endl;
A.clear();
sleep( 10 );
}
cerr << "phase 3 free matrix" << endl;
sleep( 10 );
return 0;
}
Hao Kuang , Ku