Operating System - HP-UX
1821245 Members
2982 Online
109632 Solutions
New Discussion юеВ

how to free memory in vector

 
xbin999
Occasional Contributor

how to free memory in vector

Hi all:
While I used STL in my appliaction, i got a trouble to free the memory.
My test program looks like this:
void f()
{
vector b;
cout << "push back begin..." << endl;
for ( int i = 0; i < MAXNUM; ++i)
{
b.push_back(i);
}
cout << "push back end..." << endl;
cout << b.size() << ":" << b.capacity() << endl;
b.clear();
cout << "cleared ..." << endl;
cout << b.size() << ":" << b.capacity() << endl;
vector().swap(b);
cout << "swaped ..." << endl;
cout << b.size() << ":" << b.capacity() << endl;

getchar();
}
%aCC -AA testmem.cpp -o testmem

%testmem
push back begin...
push back end...
10000000:10570698
cleared ...
0:10570698
swaped ...
0:0

The matter is that the memory still is 100M after swaped the vecotr, even the variable 'b' went out of its life cycle. It seemd that the memory would never be freeed.

Can any one help me to resolve the problem? Your help is appreciated. Thanks a lot.

Regards,
xbin999
5 REPLIES 5
Alexey Roytman
Frequent Advisor

Re: how to free memory in vector

Generally, all memory allocation mechanisms (STL vectors, malloc/free) reuse the memory. (For example, allocate 16K, check process size, free 16K, check process size, and you won't see that the process heap decreased.)

The same situation happens with STL vector. The only true solution will be to use your own allocator for STL classes.

Other solutions are not so good, and may work or not work.

1. b.resize(0);
2. vector zero; b = zero;

P.S. The behavior you see is the correct one, and defined by the STL standards.
A. Clay Stephenson
Acclaimed Contributor

Re: how to free memory in vector

You need to understand that when dynamically allocated memory is freed by a process, it is not returned to the OS but rather is made available for reuse by the same process. The memory "footprint" never actually shrinks.

There is a method to shrink the memory usage using the sbrk() system call with a negative increment but if you use that mechanism then you must take sole responsibilty for memory management --- meaning no new, delete, malloc, free --- and none of these in any library code as well.
If it ain't broke, I can fix that.
xbin999
Occasional Contributor

Re: how to free memory in vector

My application is a background service. More processes will be forked while receiving a message. So memory will be exhausted.
The program is ok on the Windows platform, but get trouble on HPUX.
Maybe I need to manage the memory by myself, not STL. But any good suggestions?
A. Clay Stephenson
Acclaimed Contributor

Re: how to free memory in vector

Forking a process is not a problem. That memory will be freed when the process terminates. It's very common practice in UNIX for a background process (a daemon) to listen for requests and fork for each new request. Each newly spawned child process handles a request and exits while the parent continues to listen for new requests. If well written there should be no memory leaks using this method.
If it ain't broke, I can fix that.
Alexey Roytman
Frequent Advisor

Re: how to free memory in vector

If you do not want to redesign your aplication (for example, to use thread pool), you have a simple option: find sources of "libmapmalloc" library (it uses mmap/munmap instead of sbrk/brk) and will really return memory to the OS.
But it's very slow.

Also, the question that arises is: have you checked, that the memory is exhausting, or you just do suppose that?

In addition, swapping with anonymous vector should be in a separate block, to give the compiler hint to destroy the vector immediately.