Operating System - Linux
1822430 Members
3033 Online
109642 Solutions
New Discussion юеВ

Vector doesn't release memory

 
kantaguo
Occasional Contributor

Vector 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)

5 REPLIES 5
Steven E. Protter
Exalted Contributor

Re: Vector doesn't release memory

Shalom,

Can't help you with the code.

Can help you be sure this program is the leaker.

http://www.hpux.ws/?p=8
Memory leak detector.

It works on HP-UX and Red Hat, so test it on both.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Vector doesn't release memory

Hi:

Memory that is free()'d in HP-UX is not returned to the operating system until the process terminates. Instead, the deallocated memory is returned to the "heap" for reuse by the owning process if it allocates memory again. Hence, the memory footprint of a process will rarely shrink, but will seems to remain static or will grow with increased allocation.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Vector doesn't release memory

Clay, SEP and JRF are correct. The freed space is put back in the heap for later reuse.

Adding a call to sbrk(2) in the outer for-loop indicates no leaks. Purify beta on IPF indicates no leaks either.

>My program can keep memory even more than 1G. How big a pool!??

Well, as big as your heap high water mark.

>HP-Unix 11.2, aCC 5.5).

I assume you are on 11.23. Note aCC5 is no longer supported. The latest is A.06.15.
kantaguo
Occasional Contributor

Re: Vector doesn't release memory

Thank you for your replies.

I have known the feature that vector will keep the memory allocated as a memory pool in order to enhance the performance of memory allocation. Sometime I use the vector to store very large data(more than 1G), but the large data isn't used frequent. It means that most of the time the memory keeped by the progess is idle. And so large memory usage let my system and platform slowdown.

Can I forbid the memory pool,let the vector free memory immediately├п┬╝

It need a special global valiable, compiler option or any other configuration?
Dennis Handly
Acclaimed Contributor

Re: Vector doesn't release memory

>And so large memory usage let my system and platform slowdown.

Do you have any proof of that? This should only waste swap space and the "idle" pages should just be paged out.

>Can I forbid the memory pool, let the vector free memory immediately? It needs a special global variable or any other configuration?

Probably not. This is a libc issue. As Clay mentioned, you could try:
mallopt(M_REL_LAST_FBLK, 1);