Why AZStd::vector do not cleared RAM?

0

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 :)

p.s.

I think i must rebuild memory allocator or somthing simular

asked 7 years ago169 views
7 Answers
0
Accepted Answer

Hey @REDACTEDUSER

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 :)

Let me know if that works!

Cheers, Colden

answered 7 years ago
0

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 :( Maybe i not correct understand you.

answered 7 years ago
0

Let me check in with our team on this :)

answered 7 years ago
0

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.

answered 7 years ago
0

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

answered 7 years ago
0

Thank you Herpaderp, i check that :)

answered 7 years ago
0

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
}
answered 5 years ago

This post is closed: Adding new answers, comments, and votes is disabled.