What is the most efficient way of freeing the memory allocated to a massive vector? : <p>I have a massive  <code>std::vector&lt;strc&gt; V</code> filled with more than 100M elements, which I use as the input of some function <code>foo</code>. Here, <code>strc</code> 
 is simply a <code>struct</code> containing three primitives. After the call to <code>foo</code> finishes, I don't have any purpose for <code>V</code>, so I want to reclaim as much memory from it as possible. Clearly <code>V.clear()</code> doesn't do the job as <code>V</code> retains its capacity. </p>

<p>I have read some suggestion advocating for the trick <code>std::vector&lt;strc&gt;().swap(V)</code>, but it looks rather gimmicky an not good practice. I'm not even sure if it does what I need!. Is there a standard way of doing this? If indeed <code>swap</code> is the best option, should I call <code>clear</code> first? Since the complexity of <code>swap</code> is constant, it seems that <code>swap</code> is literally exchanging the address of <code>V</code> with that of an empty, nameless vector. Then, what happens with all the stuff that was originally in <code>V</code>? I expect it to remain in memory but no longer accessible. Does that chunk of memory gets directly marked as free after the swap? i.e., gets somehow deallocated?</p>

<p>Other sources suggest <code>V.clear()</code> and then <code>V.shrink_to_fit()</code>. Is this a better option?</p>
