HELP: std vector clean() and memory release question ??

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
 
K

kayjean

thanks for your help.
But I still can't release all memory.

SWAP works in phase 2
but it 'doesn't' work in phase1
memory in Element still allocate in test until it exit.
but memory in ElementMatrix free where phase 2.
3.2.2-5))

#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(); std::vector<unsigned
long>().swap(element_member); }; // swap don't work
};
typedef vector<Element> ElementMatrix ;
typedef vector<Element>::iterator ElementMatrixIterator ;
int main()
{
{
ElementMatrix A;
for(int i = 0 ; i < 1000000 ; i++ )
{
Element B ;
for (unsigned long j = 0 ; j < 30 ; j++)
B.element_member.push_back( 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();
cout << A.capacity() << " ";
vector<Element>().swap(A); //swap works
cout << A.capacity() << endl;
sleep( 10 );
}
cerr << "phase 3 delete matrix" << endl;
sleep( 10 );
return 0;
}
 
D

David Hilsee

kayjean said:
thanks for your help.
But I still can't release all memory.

SWAP works in phase 2
but it 'doesn't' work in phase1
memory in Element still allocate in test until it exit.
but memory in ElementMatrix free where phase 2.

3.2.2-5))
<snip>

I can't perform any tests myself because I do not run this configuration.
I'm confused by the fact that it "works" in one instance but not in another.
Maybe someone familiar with gcc or some newsgroup that discusses gcc can
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top