简体   繁体   中英

Can I use one memory pool for multiple vectors?

Say I have a STL compliant(ish) allocator. I want multiple instances (millions) of an STL container ( std::vector ) to use the same instance of that allocator (we'll assume thread safety is guaranteed). Will this implementation work as expected?

MemoryPool<int> mypool;
std::vector<std::vector<int, MemoryPool<int>>> myvec;
myvec.assign(BIG_NUMBER, std::vector<int, MemoryPool<int>>{1, mydefault, mypool});

I also attempted:

std::vector<std::vector<int, MemoryPool<int>>> myvec;
myvec.assign(BIG_NUMBER, {mydefault});

...and got the same memory usage.

The problem is that my memory usage is blowing up with this implementation. From ~12MB with the default allocator to ~4GB with the memory pool allocator.

I'm assuming what's happening is that each vector is getting a new instance of my memory allocator, resulting in a lot of wasted storage due to the large block size of the memory pool. I reason that if they all used the same memory pool instance, the memory usage would be closer to the default allocator.

For reference, I'm using this memory pool implementation: https://github.com/cacay/MemoryPool

You cannot use said MemoryPool as an allocator for std::vector , since it is intended for fixed-size allocations:

A memory pool has just a few disadvantages:

  • Objects have a fixed size which must be known beforehand. This is usually not a problem and mostly the case if you need to allocate them in a bunch.

Also see the comment above the declaration of the allocate() function :

 // Can only allocate one object at a time. n and hint are ignored pointer allocate(size_type n = 1, const_pointer hint = 0); 

std::vector doesn't meet this constraint since it allocates memory as a single contiguous block required to fit at least capacity() elements. So it calls the allocate() function with n set to capacity() , but MemoryPool works as if n=1 was passed in. The implementation of MemoryPool doesn't even provide a protection (in the form of an assertion) against such invalid use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM