Operating System - Linux
1753365 Members
6017 Online
108792 Solutions
New Discussion

vector(STL) doesn't release memory

 
kantaguo
Occasional Contributor

vector(STL) doesn't release memory

HP-Unix 11.2
aCC 5.5

I use lots of vector(STL vector) in my source code to store the object return from database. Recently I find an issue that the vector doesn't release its memory even leaves its scope. But the issue doesn't rise at linux platform(Redhat AS3, gcc 3.2). Someone side that the vector will keep some memory as memory pool till the process die. But it seem to can't answer my question. My program can keep memory even more than 1G. How big a pool!??

I want to know why and how can I fix the problem.

My testing code is as below:

testFun()
{
string str = "aaaaaaaa";
vector aVect;
for(long i=0;i<1024*1024;i++)
{
aVect.push_back(str);
}

aVect.clear();
vector().swap(aVect);
}

int main()
{
for(int i=0;i<1024;i++)
{
testFun();
}
}

The source code above will let the memory of process rise and won't free it till the process out of life(HP-Unix 11.2, aCC 5.5).
The issue won't rise at linux(Redhat AS3, gcc 3.2)

1 REPLY 1
A. Clay Stephenson
Acclaimed Contributor

Re: vector(STL) doesn't release memory

This is normal and expected behavior and typical for most flavors of UNIX. Eventhough a destructor is invoked, the freed memory is placed on the process's heap for subsequent use by the same process. The actual memory footprint does not shrink.

The underlying system call, sbrk(), does allow for a negative growth so that memory could be returned to the system. Newer versions of the malloc(), calloc(), and realloc() functions can actually return memory to the system under very strict conditions --- if the free()'ed block was the last allocated.

UNIX ain't Linux so it is unrealistic to expect Linux behavior.
If it ain't broke, I can fix that.