I have about 20 vectors like the example below inside a buttoncontrol that will push_back data.
This buttoncontrol and all the vectors will read a .txtfile that takes up 1.5 MB.
So together the 20 vectors will fill data with a total of about 20 MB when I press the button.
So I beleive that this will take 20 MB from the RAM ?

What happens is that before I press the button. The whole application takes up: 53 MB.
When I press the button, the application will take up: 130 MB of RAM and stay like this.
Here I wonder. Shouldn´t the RAM go up to 130 MB and the drop to 53 MB again since I clear() all the vectors that store the data ?

Because if I press the button the second time now, the RAM goes up even more, to 200 MB.
It would be wonderful to hear any thoughts about this.

This way I am filling up the vectors with data.

std::vector<string> One;
One.push_back(Stuff);

This way I suppose I clear the vector from taking up RAM ?

One.clear();

Standard containers don't actually free memory they use (AFAIK). You have to replace them.

One way to do that is to swap it with itself: v1.swap( v1 ); I am shocked, however, that a 1.5 MB datafile uses 20MB memory. How exactly are you storing the data from the file?

Yes, I am also a bit shocked what fills up the memory so much. It is more than 20 MB. The program starts with 50 MB and after pressing the button with 20 vectors the program is up to 130 MB.
The file that I read is 1.5 MB and I store different things from the file in different vectors, all using the push_back method.
So the total of memory perheps could be about 20 MB (about 1.5 MB * 20 vectors).
However I wonder why it goes up to 130 MB, that is a lot more than perheps it should be about 70 MB instead.
I don´t fill upp anything else with memory except a lots of simple double and int:s.
Perheps I have 300 variables like this but they doesn´t take up to much memory though.

I am confused :) I will try this swap method a little and see what will happen...
Thanks...

Standard containers don't actually free memory they use (AFAIK). You have to replace them.

One way to do that is to swap it with itself: v1.swap( v1 ); I am shocked, however, that a 1.5 MB datafile uses 20MB memory. How exactly are you storing the data from the file?

However I wonder why it goes up to 130 MB, that is a lot more than perheps it should be about 70 MB instead.

Every std::string object that you push onto a vector has its own overhead. You can check in code what sizeof(std::string) gives to be the size of a string. That much overhead will be used for each string you store in a vector.
When you have loaded all the data, sum together the vectors' sizes (i.e. the number of strings you have stored) hence you will know how much 'extra' memory you are using.

When you call vector.clear(), the objects contained will be dropped and their destructor will be invoked i.e. the memory will be released.

I understand that this might not be so easy to know perheps. In the end of the code I do clear all vectors that I use(20 vectors) with v.clear();

I have for ex checked a vectors size() and it took up 17189 wich is in bytes I beleive.
I tried to clear() it and it did take up 0 so this seemed to work.
For the steps below I do clear(); All vectors that I use in the end. When I do this the RAM should be released after each press on the button and this should be seened in TaskManager where I follow the process.

The scenario is this:
1. When open the whole application, the application takes up 50 MB RAM.
2. When pressing the button First time the application takes up 130 MB RAM.

3. Second press on the button: 200 MB RAM
4. Third time: 230 MB
5. Fourth time: 295 MB

6. Fifth time: it drops to 165 MB ? ( I might wonder what could have happened here )

7. goto 2.


Every std::string object that you push onto a vector has its own overhead. You can check in code what sizeof(std::string) gives to be the size of a string. That much overhead will be used for each string you store in a vector.
When you have loaded all the data, sum together the vectors' sizes (i.e. the number of strings you have stored) hence you will know how much 'extra' memory you are using.

When you call vector.clear(), the objects contained will be dropped and their destructor will be invoked i.e. the memory will be released.

This article has been dead for over six months. Start a new discussion instead.