externstruct wdate_t; //some structure including several int's
typedef vector<wdate_t> v_wdate_t;
typedef map<string,v_wdate_t> m_str_vwdate_t;
typedef map<string,m_str_vwdate_t> m_str_mstrvwdate_t;
typedef map<unsignedlong,m_str_mstrvwdate_t> m_ul_mstrmstrvwdate_t;
typedef map<string,m_ul_mstrmstrvwdate_t> svc_t;
void main(){
wdate_t wdate;
svc_t svc;
...
while(true){
for(int i=0;i<100000;i++){
//getting object wdate of wdate_t type
get(i,&wdate);//just filling its fields
//inserting into container
svc[<string1>][<ulong1>][<string2>][<string3>].push_back(wdate);
//<stringi>- just variable of string type, <ulong1> - some unsigned long variable
}
...
svc.clear();//erasing all elements from container
}
...
}
I compile this under Solaris using CC-compiler, version 5.8. Program works fine, but I have memory leakage... I have done several tests and the problem appears like program doesn't free memory when I invoke svc.clear(). But after invokation of clear() method, svc contains zero elements.
With each iteration program allocates new memory block (I can monitor this with top utility).
My question is: what's wrong?
vector<> never releases the memory it allocates, even when you clear() it. (It only releases the memory
on destruction). One of my pet peeves re: vector.
jsmith, thank you for your answer.
It's strange, but under VC2005 vector successfully releases the memory by calling clear() method. Did you mean exactly CC compiler in your post?
The exact behavior of clear() is probably implementation-dependent. All it guarantees is that the size will be zero after the call, and that all destructors will be called.
If you need to guarantee that the memory will be freed, use a dynamically-allocated vector, then delete and reallocate when necessary.
But I use vector nested in map. Doesn't it mean that vector is allocated dynamically and map::clear() just implements delete for each of its vector-objects?