470,591 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

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

Hi. I have a small test.cc
g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5))
phase 1 (VSZ RSS 50844 47748 )
phase 2 (VSZ RSS 50844 47748 )
phase 3 (VSZ RSS 44696 41888 )


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
Jul 22 '05 #1
3 8463
"kayjean" <ka*****@gmail.com> wrote in message
news:e5**************************@posting.google.c om...
Hi. I have a small test.cc
g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)) phase 1 (VSZ RSS 50844 47748 )
phase 2 (VSZ RSS 50844 47748 )
phase 3 (VSZ RSS 44696 41888 )


I found that vector.clear() doesn't free the memory.
How to free the memory I used ??


http://www.gotw.ca/gotw/054.htm

See question #3 and the answer.

--
David Hilsee
Jul 22 '05 #2
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.
g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux 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;
}

"David Hilsee" <da*************@yahoo.com> wrote in message news:<iM********************@comcast.com>...
"kayjean" <ka*****@gmail.com> wrote in message
news:e5**************************@posting.google.c om...
Hi. I have a small test.cc
> g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux

3.2.2-5))> phase 1 (VSZ RSS 50844 47748 )
> phase 2 (VSZ RSS 50844 47748 )
> phase 3 (VSZ RSS 44696 41888 )


I found that vector.clear() doesn't free the memory.
How to free the memory I used ??


http://www.gotw.ca/gotw/054.htm

See question #3 and the answer.

Jul 22 '05 #3
"kayjean" <ka*****@gmail.com> wrote in message
news:e5**************************@posting.google.c om...
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.
g++ -o test test.cc (gcc version 3.2.2 20030222 (Red Hat Linux

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.

--
David Hilsee
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by cylin | last post: by
32 posts views Thread by Sambucus | last post: by
29 posts views Thread by Hagen | last post: by
10 posts views Thread by electrixnow | last post: by
11 posts views Thread by Antimon | last post: by
17 posts views Thread by toton | last post: by
32 posts views Thread by T. Crane | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.