Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

returned reference to a vecto and resident memory usage

Status
Not open for further replies.

maluk

Programmer
Oct 12, 2002
79
0
0
SG
I have code that returns a reference to a member variable. The member variable is a vector of a certin class. This class consumes [tt]5848[/tt] or more bytes.

The process is like this, I loop on the number of objects in the [tt]vector[/tt] described above using [tt]iterator[/tt]s in a [tt]for()[/tt] loop. So I have a class like so:
Code:
01 class B; // consumes 5848 or more bytes
02 class A
03 {
04     public:
05         vector<B>& getB()
06         {
07             return myB;
08         }
09   
10         void AddToList(B& b);
11         void RemoveFromList(B& b);
12     private:
13         vector<B> myB;
14 }

Line [tt]05[/tt] above returns a reference to my member variable which is a [tt]vector[/tt].
I use this in a separate class:

Code:
01 class A;
02 class MyMain
03 {
04     public:
05         void doIt()
06         {
07             vector<B>::iterator iter;
08             for(iter = myA.GetB().begin(); iter != 
09                 myA.GetB().end;)
10            {
11                if(/* condition is to delete */)
12                {
13                    iter = myA.GetB().erase(iter);
14                }
15                else
16                {
17                    ++iter;
18                }
19                ...
21                ...
22            }
23        }
24    private:
25        A myA;
26}

I have observed that by using [tt]top[/tt] command on a UNIX platform, whenever the [tt]doIt()[/tt] function gets called, the resident memory usage keeps on increasing and increasing. It seems that the used memory for the [tt]vector[/tt] was not deleted.

Can anyone explain to me why? I am at a lost here since Rational Purify for UNIX did not report any memory leaks for this though as I said, through observation using [tt]top[/tt] command the [tt]RES[/tt] (resident memory) usage keeps on increasing. It does not decrease even after a call to [tt]erase()[/tt] on line [tt]13[/tt].

Some one help!

Rome did not create a great empire by having meetings, they did it by
killing all those who opposed them.

- janvier -
 
The vector class usually keeps a pool of memory available for use, and when that pool of memory is used up, it re-allocates a larger pool of memory. So when you keep adding new objects into the vector, it grows to make room for those new objects.

When you erase an object, that object is destroyed correctly, but the memory remains available for future use to avoid constantly allocating and deallocating memory, which can be expensive. So you should not expect the memory of the vector to decrease after calling erase. TO see how many objects could fit into the allocated memory, use the capacity() member function. Generally, capacity() >= size(), and after an erase it should be capacity() > size().

Now, if you have 20 objects in the vector, then erase 10, then add 10 more back, you should probably not see a memory increase when adding the last 10, because the memory was probably still available from when you had 20 objects to start with. If your memory keeps going up and up even though, then you might have some problem with your code.

If you want to remove the excess memory used by the vector after erasing a bunch of objects, one trick that sometimes works (but not always - depends on the compiler and library implementation) is the swap trick:
Code:
void doIt()
{
  // ...
  if (myA.GetB().capacity() - myA.GetB().size() > 1000)
  {
    vector<B> tmp(myA.GetB());
    myA.GetB().swap(tmp);
  }
}
Note that you don't want to do that too often, because it makes a complete copy of the vector, but if you know that you erased a large number of objects and you want to reclaim that memory, you can try that.
 
After correction of obvious errors in your snippet (getB/GetB, end/end() - suspicious subject, eh?) and adding some stubs, I can't reproduce any memory leak conditions (on Windows, VC++ 6.0).
As uolj said, you can't decrease your process virtual memory usage by C++ delete (strictly speaking, working set may be decreased).
Also take into account that vector<> collects copies of inserted objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top