STL container memory Compression Method

Source: Internet
Author: User

Generally, Sever developers do not like to use STL containers to manage memory data because the memory collection policy of STL containers may not be very suitable for servers: Server resident memory, we hope that the used content can be quickly recycled. After inserting 10000 objects using push_back of vector, go out to the 10000 objects pop_back. The memory space of the 10000 objects applied for is not immediately released. The STL container still retains this part of the object for future use. If you really need to compress the memory of the empty vector, you can use the swap method of the vector. For example, vector A is A container that requires compression. You can create A temporary vector B and call B. swap (). because B is A temporary object, it can be released immediately, and the content of A and B is exchanged, that is, A is B. Memory compression is achieved. Attached Code Section: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>#include <vector>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> # include <iostream>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> using namespace std;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> typedef struct Block {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> char dummy [1024];
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>} Block;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> typedef vector <Block> BlkVector;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> void show_space (BlkVector * blk_vec)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif" align = "top"/> cout <"size =" <blk_vec-> size () <", capacity = "<blk_vec-> capacity () <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> int main (int argc, char * args [])
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> BlkVector bv;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> Block B;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> show_space (& bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> bv. push_back (B );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> show_space (& bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> for (int I = 0; I <1000; I ++)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> bv. push_back (B );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> show_space (& bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> while (bv. size ()> 0)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> bv. pop_back ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> show_space (& bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> BlkVector (). swap (bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/> show_space (& bv );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1SR04034-0.gif "align =" top "/>}
 

Vector: swap Public member function
void swap ( vector<T,Allocator>& vec );
Swap contentExchanges the content of the vector by the content Vec, Which is another vector of the same type. Sizes may differ.

After the call to this member function, the elements in this container are those which were in VecBefore the call, and the elements VecAre those which were in this. All iterators, references and pointers remain valid for the swapped vectors.

Notice that a global algorithm function exists with this same name, swap, and the same behavior.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.