Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::object_pool - Memory allocation problems
From: Julien Nitard (julien.nitard_at_[hidden])
Date: 2011-09-23 10:46:03


<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:

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at