Problems with releasing memory for the "vector"!

Christopher Jefferson caj@cs.york.ac.uk
Wed Apr 20 10:37:00 GMT 2005


Hao Xia wrote:
> Hi, guys
> 
> I think the best way is to attach a test program, atest.cpp.
> 
> I was releasing memory from a nested vector:
> 
> vector< vector<someclass> > F;
> 
> But never get it done...
> <snip>
> 
> As you can see, memory was not released even for non-existing variable.
> 
> Any comments?

This is just how memory allocation works.. the memory is kept in an 
internal cache, so it can be given back out without having to go and ask 
the operating system for some more memory.

Try altering your program to have the code in main twice. I did this, 
and got this output:

    RSS      VSZ %MEM COMMAND
121780   156868 15.5 ./a.out
Before clear --------------------------
    RSS      VSZ %MEM COMMAND
109552   144708 13.9 ./a.out
After clear ---------------------------
    RSS      VSZ %MEM COMMAND
121840   156996 15.5 ./a.out
Before clear --------------------------
    RSS      VSZ %MEM COMMAND
109552   144708 13.9 ./a.out
After clear ---------------------------

as you can see, modulo some very small differences, the memory is being 
reused the second time through.

I'm unsure if it is possible to force the memory to be released back to 
the operating system, and how useful such a thing would be anyway...

Chris



More information about the Libstdc++ mailing list