Object pool vs. dynamic allocation
Asked Answered
M

4

15

When should one prefer object pool over dynamically allocated objects?

I need to create and destroy thousands of objects per second. Is it by itself enough to decide in favor of object pool?

Thanks.

Mchail answered 9/8, 2009 at 9:1 Comment(1)
As usual - it depends. And if you can use an object pool, you obviously don't "need" to create and destroy things. So wee need more detail.Originality
C
11

Yes, this is enough to decide in favor of object pool.

Quoting Boost documentation

When should I use Pool?

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.

See Boost Pool library

Conscript answered 9/8, 2009 at 9:14 Comment(0)
P
9

Measure, measure, measure. Then you'll know, and you won't have to rely on speculation or guidelines.

Also, if Dirk Grunwald's CustomMalloc is still available, give it a try. It synthesizes an implementation of malloc that is tuned to the needs of a single application.

Pergola answered 9/8, 2009 at 16:10 Comment(0)
A
8

The expected cost of destructing the object, deallocation, allocation and construction is higher than the cost of reinitializing for a new use.

Aftershock answered 9/8, 2009 at 9:3 Comment(5)
I would say "may be low" - it's entirely possible (even likely) the initialisation is the expensive stage.Originality
I reformulated my answer. Reading the other answers, it seems that I'm the only one making a difference between objects pool (where one keeps a list of constructed objects for use) and memory pool (which is a memory management technique where one delay the deallocation -- and sometimes the destruction, sometimes the destruction is purely not done -- so that it is made in one step).Aftershock
I'm not sure I understand what you're saying here. I always thought that the benefit of an object pool was only memory reuse? All new objects needs to be constructed and all old objects destructed anyway - you cannot save on that time?Allbee
That's the difference I tried to hint to. What you describe is what I've always seen called "memory pools" (well, I've also seen lot of variants for that) What I call "objects pool" is a pattern where you keep already constructed objects in the pool and use them and put them pack in the pool without destructing them when you don't need them any more. With memory pool, the emphasis is on reducing the allocation/deallocation time, with objects pool, it is the construction/destruction time. Obviously, this is less generally applicable.Aftershock
Thanks - I see now, I was only considering the memory pool case.Allbee
S
5

Generally if you're creating and destroying thousands of objects a second you should at least use an object pool.

You could use a custom allocator which purely allocates objects of a specific size. Override new and pre allocate a heap specifically for your objects. Using a bit field and an array its relatively simple.

Basically a custom heap is more memory efficient if the objects are small (the heap overhead is quite high relative to small objects size); Its faster; It prevents heap fragmentation; And its easier to debug.

Strappado answered 9/8, 2009 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.