c++ - 为什么使用 Boost pool ordered_free() 而不是 free()?

标签 c++ memory-management

为什么在使用 Boost 池时会选择使用 ordered_free() 而不是 free()?假设 ordered_free() 总是 O(n)free() 应该是 O(1)。碎片较少的地方有好处吗?我的用例是在高性能服务器中使用 Boost 池,该服务器将全天运行,全天进行大量分配和取消分配。

最佳答案

The documentation回答这个问题:

An ordered pool maintains it's free list in order of the address of each free block - this is the most efficient way if you're likely to allocate arrays of objects. However, freeing an object can be O(N) in the number of currently free blocks which can be prohibitively expensive in some situations.

An unordered pool does not maintain it's free list in any particular order, as a result allocation and freeing single objects is very fast, but allocating arrays may be slow (and in particular the pool may not be aware that it contains enough free memory for the allocation request, and unnecessarily allocate more memory).

关于c++ - 为什么使用 Boost pool ordered_free() 而不是 free()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12164521/

相关文章:

c++ - 双指针动态分配

c++ - 给定两个类,我如何概率地测试等效行为

c++ - unordered_set<reference_wrapper<Ty>> 有效吗?

java - Java 表达式中子表达式的求值顺序

c++ - libstdc++ 和 libc++ : operator>> on bitset 行为差异

c++ - 在栈和堆之间动态切换

c++ - boost::program_options 的链接错误

iphone - 这个 Objective-C 嵌套 NSArray 会导致 iPhone 上的内存泄漏吗?

c++ - 在静态成员分配之前调用函数

c - 引用计数如何工作?