How do you manage object pools in your engine/game?

Started by
15 comments, last by ApEk 9 years, 6 months ago

The object pool pattern is about re-using objects by putting back wasted objects into the pool (perhaps up to an upper limit) instead of deleting them. So sub-sequent allocations are fed from the pool until those is depleted. The pool may be preset with a minimum of objects.

The pool allocation scheme is a concept where (usually fixed size) objects are allocated from a pre-allocated block of memory. Its purpose is to avoid the costs of generic new/delete or malloc/free operations in situations where the memory usage pattern is known.

Both concepts are independent; they can even be combined.

Advertisement

Thanks for all the answers. Very informative.

the old school way:

a statically allocated array of structs. each struct has an "active" field (boolean).

allocate: findfirst inactive, return index, use it.

deallocate: active=0;

there is one array for active entities, and a second for active projectiles.

very clean code. runs very fast.

runtime memory allocations are limited to local variables created on the stack, and perhaps one explicit malloc and free of a huge buffer for reading in the exe file and calculating a checkcsum.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

The only thing I'm using object pools for are my particles. I have a list of 10,000 that I keep up with. I keep up with the nextParticleIndex and when I need a particle, I use that guy and advance the counter. I setup the particle with the appropriate position, color, rotation, etc. Each particle also has an Active flag and sets this to false after the particle's duration has elapsed.

I don't bother looking for the first inactive particle, I just keep using the next guy assuming he's completed. If I have 10,000 active particles and overwrite one, I don't think any of my players will notice. :)

For everything else, I just new up and delete as needed. I doubt it will ever be an issue. If it ever is, that's when I'll look into profiling where the problem is and optimizing it then.

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

Question (C++ specific) to those using init methods in lieu of constructors: Why not use placement new/delete instead of methods that init your object?

Question (C++ specific) to those using init methods in lieu of constructors: Why not use placement new/delete instead of methods that init your object?

In a lot of cases I don't actually call the init method, usually when I know required values are build in next code block thus avoiding unneeded constructor/method call and/or stack allocation for temp values to pass on to constructor/method. This could still be achieved with placement but then it becomes obfuscated and less obvious when reading true the code months later, so boils down to personal preference I guess. (Pod objects only, for objects with resources that require to be managed following RAII is a must in my mind and placement seems like a nice option.)

Tom Robbins: "There's a tendency today to absolve individuals of moral responsibility and treat them as victims of social circumstance." - Don't be that victim, take responsibility and be what you want to be.

A simple pool is much safer, but can suffer from fragmentation. It is a double linked list of memory fragments.
This produces an overhead on each memory allocation, you attach a header to each allocation. So each alloc consumes requested memory + header size bytes of memory.
When you free a block of memory, you look at the last and next blocks. If either of those is free you join this allocation to it... kind of hard to explain.


Note that the memory overhead caused by link pointers can be safely eliminated through the proper use of unions and type punning.

This topic is closed to new replies.

Advertisement