回收释放的对象
假设我需要经常在堆上分配和删除对象(任意大小),如果我不删除这些对象,而是将其返回到某个“池”以供以后重用,是否有任何性能优势?
它会通过减少堆分配/释放来带来好处吗?或者它会比内存分配器性能更慢,因为“池”需要管理指针的动态集合。
我的用例:假设我创建一个基于链表的队列容器,并且该列表的每个节点都在堆上分配,因此每次调用 push() 和 pop() 都会分配和释放该节点:
`
template <typename T> struct QueueNode {
QueueNode<T>* next;
T object;
}
template <typename T> class Queue {
void push(T object) {
QueueNode<T>* newNode = QueueNodePool<T>::get(); //get recycled node
if(!newNode) {
newNode = new QueueNode<T>(object);
}
// push newNode routine here..
}
T pop() {
//pop routine here...
QueueNodePool<T>::store(unusedNode); //recycle node
return unusedNode->object;
}
}
`
suppose I need to allocate and delete object on heap frequently (of arbitrary size), is there any performance benefit if instead of deleting those objects, I will return it back to some "pool" to be reused later?
would it give benefit by reduce heap allocation/deallocation?, or it will be slower compared to memory allocator performance, since the "pool" need to manage a dynamic collection of pointers.
my use case: suppose I create a queue container based on linked list, and each node of that list are allocated on the heap, so every call to push() and pop() will allocate and deallocate that node:
`
template <typename T> struct QueueNode {
QueueNode<T>* next;
T object;
}
template <typename T> class Queue {
void push(T object) {
QueueNode<T>* newNode = QueueNodePool<T>::get(); //get recycled node
if(!newNode) {
newNode = new QueueNode<T>(object);
}
// push newNode routine here..
}
T pop() {
//pop routine here...
QueueNodePool<T>::store(unusedNode); //recycle node
return unusedNode->object;
}
}
`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
池化是一种非常常见的技术,可以避免频繁的分配和释放。有些将其视为一种设计模式。
通常存在现有的实现,因此重新发明轮子没有任何好处。
您可能想看一下问题对象池与动态分配
Pooling is a very common technique to avoid frequent allocations and deallocations. Some treat it as a design pattern.
There are typically existing implementations, so there is no benefit to reinventing the wheel.
You may want to take a look at the question Object pool vs. dynamic allocation
当我问 这个问题。 答案可能对您很有洞察力,尤其是那些解决内存碎片问题的答案。
I had similar concerns when I asked this question. The answers may be insightful to you, especially those that address the concerns of memory fragmentation.
您可以查看 Boost 对象池< /a> -- 供想法、参考或最佳使用 :>
You might take a look at Boost object pool -- for ideas, reference, or best for usage :>
这是一个特别有用的工具,可以使内存分配更加确定。如果您预先分配生成池的大块,它还会减少内存碎片。
This is a particularly useful tool to make memory allocation more deterministic. It also reduces memory fragmentation if you preallocate the large blocks from which the pool is generated.
根据您的运行时库,您可能在许多情况下都有一个“足够好”的分配器。也就是说,如果您可以证明您有一个特殊的用例,或者 libc 中的 malloc 实现很差,那么您应该只为您的应用程序构建一个池分配器。
由于 Doug Lea 的大部分工作都存在于 GNU 库中,因此您可能想在 内存分配器。
Depending on your runtime library, you may have a 'good enough' allocator for many cases. That is, you should only build in a pool allocator for your application if you can demonstrate that you have a special use case, or a poor implementation of malloc in libc.
Since much of Doug Lea's work is present in the GNU lib, you might want to read about his experiences in A Memory Allocator.