Why AZStd::vector do not cleared RAM?

I try working with AZStd::vector and found some interesting thing how i can clear memory after using AZStd::vector ?

	void test()
{
AZStd::vector<int> tmp;
tmp.resize(4000000 * 512);
tmp.clear();
}

How i understand clear() removing all objects but vector do not give back to system his memory. In this example i have local variable and after function finish “tmp” must be destroing but ~ 8gb of RAM not will be free. How i can fix that ? Or i something not correctly understand ? Any help will be nice :slight_smile:

p.s.

I think i must rebuild memory allocator or somthing simular

Hey @Anatoliy,

AZStd::vector, while it appeases most C++ standards for std::vector, actually contains some helpful tweaks.

To actually remove the memory, after calling clear(), you can call our extension tmp.shrink_to_fit() to reduce the allocated size of the container to exactly fit the number of elements it contains (in this case, 0).

We don’t free the memory for you on clear() because often when clearing, the vector just gets filled up again, and not deallocing saves a few allocations later. Combine this with shrink_to_fit (and set_capacity, which is what shrink_to_fit calls under the hood), and you get extreme control over the amount of memory you’re using while also reducing allocation calls.

Worth noting, if you have a vector containing a large number of objects that you expect to stick around for a while, it may be worth calling shrink_to_fit() after populating it to avoid excess memory usage. Like most containers, vector will over-allocate when it reaches capacity to avoid allocating on every insertion (which would be super expensive). I would not recommend shrink_to_fit’ing, however, if your vector will be short-lived because the extra allocation could add performance cost that outweighs the benefit of the smaller memory footprint. This is totally up to you though, so if your data suggests that the memory usage is indeed too high, you at least have the option :slight_smile:

Let me know if that works!

Cheers,
Colden

Hi Colden, thanks for answer, if i correctly understand i do that

   void test()
{
AZStd::vector<int> tmp;
tmp.resize(4000000 * 512);
tmp.clear();
tmp.set_capacity(0);
tmp.shrink_to_fit();
}

and in Windows Task Manager i don’t see any difference between previous example :frowning: Maybe i not correct understand you.

Let me check in with our team on this :slight_smile:

Maybe because it puts the array into a pre-allocated pool, because allocating the memory when you initialize the vector would be too costly for game code.

You might wanna check the OSAllocator to see whats going on.

Thank you Herpaderp, i check that :slight_smile:

The other day I returned to the problem about free memory after using. And Found this:

void test()
{
AZStd::vector<int> tmp;
tmp.resize(4000000 * 512);
tmp.clear();
tmp.set_capacity(0);
tmp.shrink_to_fit();

AZ::AllocatorManager::Instance().GarbageCollect(); // <<< after this command all ok
}