Boost::object_pool - Memory allocation problems

#include <boost/pool/object_pool.hpp> class test { test(void){}; public: std::vector<const int*> m_a1; int* m_b1; int* m_b2; const int* m_c1; const int* m_c2; const std::string* m_d1; unsigned int m_e1; unsigned int m_e2; unsigned char m_f1; unsigned char m_f2; unsigned char m_f3; unsigned char m_f4; }; static boost::object_pool<test> s_my_pool; int main(int argc, char* argv[]) { int max = 2097120; for(int i = 0; i < max; i++) { test* tst_c = s_my_pool.construct(test()); } Consider the code above. Compiled with Visual Studio 2010. The purpose of the class is not important. It is its size (48 bytes) that matters. The goal is to use object_pool to minimize memory usage. When running this code the memory rises with just a few bytes more than the needed 48 bytes * 2.097.120 = 100.661.760 bytes, hence the object_pool seems to be working fine. But if I increase the number of elements created by one (to 2097121) the memory use rises to a massive 200.000.000+ bytes. It seems that the object_pool has reached a threshold and that the strategy then is to double the allocated memory. But this can't be right, can it? If so the object_pool is useless because it's using more memory than a simple test* tst = new test(); would have done. Why is object_pool acting like this and is there a way to change the behavior? PS! The threshold value is not exactly the same in another application I have made, but it is pretty close. Best Regards Christian Berg

Hello, <snip />
Why is *object_pool* acting like this and is there a way to change the behavior?****
PS! The threshold value is not exactly the same in another application I have made, but it is pretty close.
This is an "amortized constant time" optimization. i.e. object pools allocates many object at once everytime it's full. The ratio is exponential (allocates twice the memory each time). This allows very fast inserts most of the time. It is much faster than allocating new memory each time. STL containers (vector...) use the same approach. Vendors may change the ratio (i.e. Microsoft uses 1.5 instead of 2). If you add more elements to your pool, it will not grow. Sadly, object_pool seems to be missing a constructor taking a size as a parameter to avoid that kind of problems (but there maybe another way around it I am not aware of). I guess that object_pool is _not_ optimized to minimize memory usage, but to minimize the number of heap allocations (that are very slow). Here is a link to give you more info: http://matetelki.com/blog/?p=114 Regards, Julien
participants (2)
-
Christian Berg
-
Julien Nitard