boost内存管理学习笔记2
1.什么是内存池?
Pool allocation is a memory allocation scheme that is very fast, but limited in its usage.
池是一种内存分配机制,可以在某些条件下快速分配内存。
2.为什么要使用内存池?
Using Pools gives you more control over how memory is used in your program. For example, you could have a situation where you want to allocate a bunch of small objects at one point, and then reach a point in your program where none of them are needed any more. Using pool interfaces, you can choose to run their destructors or just drop them off into oblivion; the pool interface will guarantee that there are no system memory leaks.
使用池可以更好地管理程序内存使用,例如,当你想在某处代码中分配一小块对象,而到达另一处代码时不再需要这块内存,就可以使用池。使用池接口后,可以运行析构函数或者直接无视他们,池接口会保证不会产生内存泄露。
3.什么时候需要使用内存池?
Pools are generally used when there is a lot of allocation and deallocation of small objects. Another common usage is the situation above, where many objects may be dropped out of memory.
In general, use Pools when you need a more efficient way to do unusual memory control.
池通常用于处理分配释放大量小对象,或者如上面那种情况,很多对象不再使用,不需要存在于内存。通常用池可以更高效地控制内存。
4.怎样选择池分配器?
pool_allocator is a more general-purpose solution, geared towards efficiently servicing requests for any number of contiguous chunks.
fast_pool_allocator is also a general-purpose solution but is geared towards efficiently servicing requests for one chunk at a time; it will work for contiguous chunks, but not as well as pool_allocator.
If you are seriously concerned about performance, use fast_pool_allocator when dealing with containers such as std::list, and use pool_allocator when dealing with containers such as std::vector.
pool_allocator是最常见的方式,可以高效处理任意数量连续数据块分配请求,fast_pool_allocator也较为通用,适用于每次分配一块内存,分配连续块没有pool_allocator性能好。如果非常关注性能,可以在std::list类容器中使用fast_pool_allocator,std::vector类容器中使用pool_allocator