• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# A STL Vector problem...

## Recommended Posts

The problem that I run into was:Should I make some on level entering load up some Poly Data and then delete the memory used for the previos levels so I could save up some level data and use only what I need... The thing is that I use a stl vector for the levels vector Level; ......//I push back 2 levels... Level.push_back(); Level.push_back(); ......//I load them up... //Doesnt matter whats here but its big(Around several MB) The thing is that When I look on my "System Monitor"(Accesories->System Tools ->System Monitor),and choose Alocated Memory all over the system,I see around 150-200 everytime(RAM and Swap offcourse, out of my 256 MB).I use up to 40-50 MB of RAM . Its pretty much OK now but If I am gonna make something bigger I would like to use only what I need(So its 1 level at a time). Youre answer until here would be "Use the Level.clear() Functions" . I logged the memory events and look what I get (The // are my commentaryes): Memory Manager: Allocated memory 185339904//Here is what It is when its not launched... 185339904//I left about 10 secs for each event... 185339904 185339904 185339904 185339904 190033920 209408000//Acclelration rises because I launch it... 220532736 222072832 226603008//The Maximum Point...Now everything is loaded... 226603008//And the Amount is:41MB of RAM Used. 226603008 226603008//I stood almost 10 secs here to see the "decrease"... 226603008 226603008 226603008 226603008 226205696//In here I Clear()//ed the stl Level Vector...Which makes more than 1/2 of all the 226205696//Allocated Memory and it actually rises...How come ? 226205696 226205696 226205696 226205696//I stood almost 10 secs here too to see if there was any change . 226205696 226205696 226205696 226205696 212185088//Deacceleration...in Memory Allocation...because I just quit... 184213504//And on to the almost initial value...The spokyer thing is that in the end 184213504//theres a much smaller quantity of Memory used...Hows that ? 184213504 184213504 184213504 184213504 184213504 184213504 184213504 So if Clear() Doesnt work what should I use ? Or I should keep it as small as possible and not try to load all things together...? I tryed to call the Destructor but it doesnt work, the compiler gives me an error.How can I call the Destructor ? pop_back() is the same thing...nothing . You can test this yourself...even with something smaller... Heres something you can try(or even make it bigger so you can further test it) #include #include #include <vector> typedef struct{ int a[100]; float c[100]; }HUGE; void main() { vector I; for(int y=0;y<10000;y++) I.push_back(); int h;cin>>h; I.clear();//If you use pop_back() you can see something interesting: //Even if poped the value and the vector element is still there! cin>>h;//I do it twice because I constantly look at the "System Monitor". } If you have WinXP you can see exactly how much you use up...Try some and tell me whats the almighty answer ?

##### Share on other sites
vector<LEVEL> Level; (so ppl don''t need to look in the src)

std::vector::clear() will not necessarily free the memory allocated, efficiency would suggest that when you .clear() something you are planning on filling the vector again later so don''t de-allocate the memory (just set the size to 0).

What exactly are you pushing onto the vector when you call push_back?

Do actually destroy the memory you need to destroy the vector. I''m assuming you have the vector as global. In this circumstance you won''t be able to destroy it until the program quits. What you could do is dynamically allocate the vector:

std::vector<LEVEL>* Level;

int main....
{
Level = new std::vector<LEVEL>

Level->push_ba... etc.

// done with the list, destroy the vector
delete Level;
}

##### Share on other sites

  template<typename T>void shrink_vector (vector<T>& _vec){        // temporary storage	vector<T> _temp;        // allocate the exact amount of required elements	_temp.reserve(_vec.size());        // copy contents (*WARNING* requires properly         // written assignment operator (=) for T!)	_temp.insert(_temp.begin(), _vec.begin(), _vec.end());        // swap the vectors	_vec.swap(_temp);}

##### Share on other sites
Im gonna put all my levels there anyhow until you I fill 1 GB of Swap is there isnt memory and thanks anyhow...

• ### Forum Statistics

• Total Topics
628285
• Total Posts
2981836

• 10
• 10
• 10
• 11
• 17