Problems with releasing memory for the "vector"!!!

Problems with releasing memory for the "vector"!!!

Hi, guys

I think the best way is to attach a test program, atest.cpp.

I was releasing memory from a nested vector, but never get it done...

My results are:

RSS VSZ %MEM CMD
1348 4664 0.0 -bash
2024 8676 0.0 vim atest.cpp
119560 121124 5.7 a.out
664 2624 0.0 ps -orss,vsize,%mem,cmd
Before clear --------------------------

RSS VSZ %MEM CMD
1348 4664 0.0 -bash
2024 8676 0.0 vim atest.cpp
107320 108836 5.1 a.out
664 2624 0.0 ps -orss,vsize,%mem,cmd
After clear ---------------------------

As you can see, memory was not released even for non-existing variable.

My platform: Xeon 2.4Ghz, Redhat9, kernel 2.4.21, gcc version 3.2.2 20030222, Intel C++ compiler 8.0.

Any comments?

Thanks!
Johnix

Code:

// atest.cpp ------------------------------------

#include 
#include 
#include 

#define MEGA 0x100000

using namespace std;

// The user defined class
class face {
  int *p;
public:
  // default constructor
  face(){
    p = NULL;
  }

  // copy constructor
  face(const face& A){
    if( A.ready() ) {
      p = new int [10];
      for(int i=0; i<10; ++i)
        *(p+i) = *(A.ready()+i);
    }
  }
  
  // some function to return p
  int* ready() const{
    return p;
  }
  
  // optional memory allocation
  void setup(){
    if(!p) p = new int [10];
    for(int i=0; i<10; ++i)
      p[i] = i;
  }
  
  // destructor
  ~face(){
    delete[] p;
  }
};

// try to clean a local variable F
void tryclean()
{
  vector< vector > F; // local variable

  F.resize(MEGA);

  {
    face tmp;
    tmp.setup();

    vector< vector >::iterator i;
    for(i=F.begin(); ipush_back(tmp);
      i->push_back(tmp);
    }
  }

  system("ps -orss,vsize,%mem,cmd");
  cout << "Before clear --------------------------

"
       << flush;

  F.clear();
  vector< vector >().swap(F); // Not really necessary!

}

// main function
int main()
{
  tryclean();
  system("ps -orss,vsize,%mem,cmd");
  cout << "After clear ---------------------------
";

  return 0;
}

1 post / 0 new
For more complete information about compiler optimizations, see our Optimization Notice.