Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I was going to implement an object pool for my particle system in Java, then I found this on Wikipedia. To rephrase, it says that object pools aren't worth using in managed languages like Java and C#, because allocations take just tens of operations as compared to hundreds in unmanaged languages like C++.

But as we all know, every instruction may hurt game performance. For example, a pool of clients in an MMO: the clients won't enter and exit the pool too fast. But particles may get renewed tens of times in a second.

The question is: is it worth it to use an object pool for particles (specifically, those that die and get recreated fast) in a managed language?

share|improve this question

1 Answer 1

up vote 14 down vote accepted

Yes, it is.

Allocation time isn't the only factor. Allocation can have side-effects, such as inducing a garbage collection pass, which can not only impact performance negatively it can also impact performance unpredictably. The specifics of this will depend on your language and platform choices.

Pooling also generally improves locality of reference for the objects in the pool, for example by keeping them all in contiguous arrays. This can improve performance while iterating the contents of the pool (or at least the live portion thereof) because the next object in the iteration will tend to already be in the data cache.

The conventional wisdom of trying to avoid any allocations whatsoever in your innermost game loops still applies even in managed languages (especially on, for example, the 360 when using XNA). The reasons for it just differ slightly.

share|improve this answer
    
+1 But, you didn't touch on whether it's worthwhile when using structs: basically it's not (pooling value types achieves nothing) - instead you should have a single (or possible a set of) array to manage them instead. –  Jonathan Dickinson Mar 7 '12 at 17:04
2  
I didn't touch on the struct thing since the OP mentioned using Java and I am not as familiar with how value types/structures operate in that language. –  Josh Petrie Mar 7 '12 at 17:08
    
There are no structs in Java, only classes (always on the heap). –  Brendan Long Mar 7 '12 at 23:57
    
Thanks Josh! I'll wait until next day before accept it, to see if I can get more answers. –  Gustavo Maciel Mar 8 '12 at 1:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.