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!