Use and test the open-source C ++ function library Boost memory pool

Source: Internet
Author: User

The Boost library is a portable open-source C ++ function library. Given that STL (Standard Template Library) has become an integral part of the C ++ language, it is no exaggeration to say that, boost is currently the most influential universal C ++ library. The Boost library is initiated by members of the C ++ Standards Committee Working Group. Some of the content is expected to become the content of the next-generation C ++ standard library and is a quasi-standard library.
The Boost memory pool is an open-source C ++ library provided by boost for memory pool management. As a library with a large impact on Boost, the Pool has been widely used.
1. what is a memory pool "pool" is a design pattern that is often used in computer technology. Its connotation is to apply for the core resources frequently used in the program first, put it in a pool and managed by the program itself, which can improve resource usage efficiency and ensure the number of resources occupied by the program. The frequently used pool technology includes the memory pool, thread pool, and connection pool, especially the memory pool and thread pool.
Memory Pool is a dynamic Memory allocation and management technology. Generally, programmers are used to directly use APIs such as new, delete, malloc, and free to allocate and release memory, due to the size of the applied memory block, frequent use may cause a large amount of memory fragments, thus reducing the performance of the program and the operating system. The memory pool applies to allocate a large block of memory (memory pool) for backup before actually using the memory. When the programmer applies for the memory, a dynamic allocation is obtained from the pool, when the programmer releases the memory, the released memory is placed into the pool and merged with the surrounding idle memory blocks as much as possible. If the memory pool is not enough, the memory pool will be automatically expanded to apply for a larger memory pool from the operating system.
Memory Pool application scenarios the early memory pool technology was designed to solve the program that frequently applied for and released memory blocks of the same size, therefore, some memory pools in the early stages were organized by memory block linked lists of the same size.
The Boost memory pool has no limit on the size of memory blocks. Therefore, the Boost memory pool is applicable to long-running programs that frequently and dynamically apply to release memory. This can effectively reduce memory fragments and improve program running efficiency.
The pool library for installing Boost is provided in the form of a C ++ header file. No installation is required, and no lib or dll files are available, you only need to include the header file in your C ++ project. The latest Boost version can be downloaded at http://www.boost.org.
2. memory Pool features 2.1 No Memory leakage correct use of the memory pool application and release functions will not cause memory leakage, more importantly, even if the incorrect use of the application and release functions, the memory in the memory pool will be automatically released at the end of the process, without causing system memory leakage.
2.2 The applied memory array is not filled. For example, if the memory size of an element is A, if the element array contains n elements, the memory size of the array must be A * n, there is no extra memory to fill the array. Although each element may contain something to fill.
2.3 The location of any array memory block is consistent with the location of the memory block allocated using operator new [], which indicates that you can still use algorithms that use array pointers to calculate the memory block location.
2.4 The memory pool is faster than the direct use of the system's dynamic memory allocation. This is probably because, not every moment, each memory pool is faster than the direct use of new or malloc. For example, when the program uses the memory pool, the memory pool is in the full state, so this memory application will cause the memory pool to expand itself, which is certainly slower than simply adding a new memory. But in most cases, the memory pool is much faster than new or malloc.
3. memory Pool Efficiency Test 3.1 Test 1: continuous application and continuous release respectively use the memory pool and new continuous application and the continuous release of a large number of memory blocks, and compare the running speed. The Code is as follows: # include "stdafx. h "# include <iostream> # include <ctime> # include <vector> # include <boost/pool. hpp> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost;
Const int MAXLENGTH = 100000;
Int main ()
{Boost: pool <> p (sizeof (int); int * vec1 [MAXLENGTH]; int * vec2 [MAXLENGTH];
Clock_t clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Vec1 [I] = static_cast <int *> (p. malloc () ;}for (int I = 0; I <MAXLENGTH; ++ I)
{P. free (vec1 [I]); vec1 [I] = NULL ;}
Clock_t clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Vec2 [I] = new int;} for (int I = 0; I <MAXLENGTH; ++ I)
{Delete vec2 [I]; vec2 [I] = NULL ;}
Clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Return 0;} test environment: VS2008, WindowXP SP2, Pentium 4 CPU dual-core, 1.5GB memory.
Conclusion: When 0.1 million blocks of memory are continuously applied for and released, the time consumed by the memory pool is 47.46% of the time consumed by the new memory pool.
3. memory Pool Efficiency Test 3.1 Test 1: continuous application and continuous release respectively use the memory pool and new continuous application and the continuous release of a large number of memory blocks, and compare the running speed. The Code is as follows: # include "stdafx. h "# include <iostream> # include <ctime> # include <vector> # include <boost/pool. hpp> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost;
Const int MAXLENGTH = 100000;
Int main ()
{Boost: pool <> p (sizeof (int); int * vec1 [MAXLENGTH]; int * vec2 [MAXLENGTH];
Clock_t clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Vec1 [I] = static_cast <int *> (p. malloc () ;}for (int I = 0; I <MAXLENGTH; ++ I)
{P. free (vec1 [I]); vec1 [I] = NULL ;}
Clock_t clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Vec2 [I] = new int;} for (int I = 0; I <MAXLENGTH; ++ I)
{Delete vec2 [I]; vec2 [I] = NULL ;}
Clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Return 0;} test environment: VS2008, WindowXP SP2, Pentium 4 CPU dual-core, 1.5GB memory.
Conclusion: In the continuous application and continuous release of 0.1 million block memory 3.2 Test 2: repeated application and release of small block memory code: # include "stdafx. h "# include <iostream> # include <ctime> # include <vector> # include <boost/pool. hpp> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost;
Const int MAXLENGTH = 500000;
Int main ()
{Boost: pool <> p (sizeof (int ));
Clock_t clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Int * t = static_cast <int *> (p. malloc (); p. free (t);} clock_t clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{Int * t = new int; delete t;} clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Return 0;} test results:
Conclusion: When the memory pool time is 0.5 million of the time consumed by the new memory pool, the time consumed by the memory pool is 64.34% of the time consumed by the new memory pool.
3.3 Test 3: repeatedly applying for and releasing the C ++ Object C ++ must not only perform memory operations but also call the constructor and analysis functions when dynamically applying for and releasing the C ++ object. Therefore, it is necessary to test the memory pool of the C ++ object.
The Code is as follows: # include "stdafx. h "# include <iostream> # include <ctime> # include <vector> # include <boost/pool. hpp> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost;
Const int MAXLENGTH = 500000; class A {public: ()
{M_ I ++ ;}~ A ()
{M_ I --;} private: int m_ I ;};
Int main ()
{Object_pool <A> q;
Clock_t clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{A * a = q. construct (); q. destroy ();}
Clock_t clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Clock_begin = clock (); for (int I = 0; I <MAXLENGTH; ++ I)
{A * a = new A; delete a;} clock_end = clock (); cout <"program running" <clock_end-clock_begin <"system clock" <endl;
Return 0;} test results:
Conclusion: When 0.5 million C ++ objects are repeatedly applied for and released, the time consumed by using the memory pool is 112.03% of the time consumed by using new. this is because the construct and destroy functions in the memory pool increase the number of function calls. In this case, performance optimization cannot be achieved when the memory pool is used.
4. Boost memory pool categories Boost memory pools are divided into four categories according to different concepts. The difference between the two concepts leads to such classification.
First, the Object Usage and Singleton Usage are different. Object Usage means that each memory pool is an Object that can be created and destroyed. Once the memory pool is destroyed, all the memory allocated by it will be released. Singleton Usage means that each memory pool is a statically allocated object and will not be destroyed until the end of the program. This also means that such a memory pool is safe with multiple threads. Only the release_memory or purge_memory method can be used to release the memory.
Second, the memory overflow processing method. The first method is to return NULL, which indicates that the memory pool overflows. The second method is to throw an exception, which indicates that the memory pool overflows.
According to the above idea, boost memory pools are divided into four types.
4.1 Pool is the memory Pool of an Object Usage. If overflow occurs, NULL is returned. 4.2 object_pool is similar to the pool. The only difference is that when the allocated memory is released, it will try to call the analysis function of this object.
4.3 singleton_pool is a Singleton Usage memory pool. If it overflows, NULL. 4.4 pool_alloc is returned. If it overflows, an exception is thrown.
5. Principle and solution of memory pool overflow 5.1 the memory pool that inevitably overflows simplifies many memory operations and avoids program damages caused by memory usage errors. However, when using a memory pool, the most important thing to note is to handle the memory pool overflow.
There is no memory that does not overflow. Check the following code: # include "stdafx. h "# include <iostream> # include <ctime> # include <vector> # include <boost/pool. hpp> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost;
Int _ tmain (int argc, _ TCHAR * argv [])
{Clock_t clock_begin = clock (); int iLength = 0; for (int I = 0; ++ I)
{Void * p = malloc (1024*1024 );&

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.