i've encountered the following problem, and i'm not really sure whether i am wrong or its a really weird bug. I fill a huge array of strings and want it to be cleared at a certain point. Here's a minimal example
#include <string>
#include <vector>
#include <unistd.h> //sleep
#include <iostream>
int main(){
{
std::vector<std::string> strvec;
for(long i = 0; i < 1000000; ++i){
std::string out = "This is gonna be a long string just to fill up the memory used by this fucking pthread\n";
strvec.push_back(out);
}
std::cout << "finished loading 1st\n";
sleep(10); // to monitor any changes
}
std::cout << "out of scope\n";
sleep(10);
return 0;
}
My Problem is, if i monitor memory usage with 'top', the memory usage decreases just by a very small amount (i think its probably the vector overhead), but the most seems not freed. How comes? I tested the same scenario with 'long long', but here everything went right.
The std::vector reference states, that, if the contained values are no pointers, the destructors are invoked. Seems not true for string though...
I appreciate every answer.
(for convenience: i'm using debian linux 64Bit with g++ 4.7.2)
EDIT: thanks for the answers so far.
by now i have profiled heap usage with vagrind massif, and (yeah, actually as expected) it gets properly freed at the point it should. But why do i in fact see a change in usage with a huge integer, but not with the strings (also whithin top)?
I need to be a little considerable about that, because i need to be able to free my memory at certain times for a multithreaded server application, which will probably run several weeks or more without being restarted. When do i actually know when the C++ memory manager decides to give some mem back to the OS?