I’n my application I’m using pools to speed up allocation of certain types of resources:
e.g.
tbb::concurrent_unordered_map<size_t, tbb::concurrent_bounded_queue<resource>> pools;
std::shared_ptr<resource> create_resource(size_t size)
{
auto pool = pools[size];
std::shared_ptr<resource> resource;
if(!pool->try_pop(resource))
resource.reset(new resource(size));
return std::shared_ptr<host_buffer>(resource.get(), [=](resource*)
{
pool->push(resource);
});
}
This works well, however I’m having some issues with high memory usage.
If the usage of resources changes in my application I have a lot of pre-allocated resources that are not needed and only taking memory space.
I would need some kind of strategy which would allow me somehow to detect when a certain pooled resource is not expected to be allocated again and dynamically adapt the pools, e.g. if a pool has not had a size less than 2 for more than a second then one resource is released.
Does anyone have any suggestions in regards to what strategies can be used to minimize memory usage while still keeping most the extra performance of pooling resources.
I solved it by keeping statistics of pool usage and flush it every second.