473,395 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 8611
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: cylin | last post by:
Dear all, We know that a vector can increase its capacity. Does it mean that system will allocate more memory to fit the value of capacity? If yes, then we maybe cost memory if capacity is...
9
by: fudmore | last post by:
Hello Everybody. I have a Segmentation fault problem. The code section at the bottom keeps throwing a Segmentation fault when it enters the IF block for the second time. const int...
32
by: Sambucus | last post by:
Hi group! I am using C++ and java with JNI to get some text in a RICHEDIT to my java program. I do so by accessing a C++ method every second. It all works fine except that it leaks memory every...
29
by: Hagen | last post by:
Hello, in a recent thread "speed of vector vs array" I read about the problem of the slow acces by addressing vector elements by indexing, unfortunately I see no workaround in my case. My...
10
by: electrixnow | last post by:
I have the following test code that opens and reads a file. The file is over 19000 lines long. The file contains two fields on each line: "text,text\n" I have placed a test that for loops up to...
6
by: Pablo | last post by:
Hello, I am writing a windows application using C++ and BorlandBuilder 6 compiler. It is an event driven program and I need to create objects of some classes written by me. One of the classes...
11
by: Antimon | last post by:
Hi, As a c++ newbie, i'm trying to figure out stuff mysself and i have a problem, thougt someone could help. Here's the code: #include <iostream> #include <vector> using namespace std;
17
by: toton | last post by:
Hi, I am using a vector to reserve certain amount of memory, and reuse it for new set of data, to avoid reallocation of memory. so the call is something like vector<my_datav;///the temporary...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.