0

When defining a vector without prior reservation/allocation of memory for the vector, and filling it via push_back(), eventually the total memory-allocation footprint for the vector (as I have observed with Xcode Instruments) is considerable larger than the contents of the vector itself.

This is a problem since I am creating a huge number of vectors like this. At one point I know that the vectors will remain constant, and I'm wondering if there exists a way to "prune" the memory allocated to a vector beyond its actual size.

I'm aware that excess allocations are done to create a buffer and minimise the risk of having to relocate the vector if it grows into already occupied memory (correct?). But, as said, I know that the concerned vectors will remain constant.

QUESTION: I have a vector V. From some point on, I know the vector will remain constant. I therefore wish to "prune" the "memory allocation footprint" around V in order to eliminate the excess reserved memory and, thereby, minimise the fragmentation of memory. Can this be done?

I was hoping for something like

V.finalise(); // * Does not exist *
V.prune();    // * Does not exist *

I tried "hacks" like

vector<T> VC // VC will be used to "copy-out" results from inner scope
{
    vector<T> V = f(); //f() builds V gradually via push_back(), no constructor used inside f()
    VC{V}              // Constructor-copy a "clean and tight" new vector copy of V
} // Destroy "messy" V leaving scope and keep a clean VC

or

vector<T> V = f();
vector<T> VC{V}
V.erase(); // Destroy "messy" V and keep a clean VC

Neither of which seem to work: In all of the subsequent code, the excess allocations, associated with the creation of V inside the function f(), remain.

Follow up QUESTION: Even if in my test-running, the excess allocations remain, I suppose that if and when needed, this memory will be able to be reclaimed for the same process (i.e. running program). Is that correct?

Thanks for any answers on this newbie-to-memory-allocation-question.

edit/update

SOLUTION (put here for visibility since question is closed due to dupe)

 std::vector<T>::shrink_to_fit() // c+11

does the job as explained in one of the answers: https://stackoverflow.com/a/1111311/11608725 to the dupe (reduce the capacity of an stl vector) of the present question.

mrchance
  • 1,133
  • 8
  • 24
  • Programs don't give memory back to the OS. If you pre-allocate your vectors you can reduce the amount of intermediate allocations – Botje Oct 02 '20 at 22:20
  • There's a way using [`std::vector::shrink_to_fit()`](https://en.cppreference.com/w/cpp/container/vector/shrink_to_fit), but it only will give allocated memory within your process, not to the OS, @Botje is right about that. – πάντα ῥεῖ Oct 02 '20 at 22:24
  • thanks to both, i think https://stackoverflow.com/a/1111311/11608725 and shrink_to_fit() fits the bill – mrchance Oct 02 '20 at 22:25

0 Answers0