XNA UK User Group

A helping hand for bedroom coders throughout the land.
in

XNAGoodies

Articles and thoughts on working with the Microsoft XNA Framework.

Object Pools and Garbage Collection

Advice From The Swamp has a new post about object pools, it includes a nicely implemented generic pool class that I suggest you take a look at. To add a little more background and depth to the article you should also read this article from the Compact Framework team, which explains some of the differences between the desktop and Compact Framework/Xbox 360 garbage collectors.

While pooling objects is very useful you need to carefully manage the size of your pools. The Compact Framework article has this to say about live objects and latency.

"We find that GC latency is approximately linear to the number of live objects… then add the cost of heap compaction on to that. Our benchmarks show that the difference between deep object hierarchies vs. shallow ones is negligible, so it’s mostly the number of objects that matter."

Which means that if you have a large number of pooled objects and a garbage collection happens your pooled objects could actually cause the garbage collection to take longer. So, pool your objects but make sure that you carefully monitor the size of pools you need. Also use some of the other techniques that Compact Framework article mentions such as using arrays of value types - a single array of 10,000 unboxed value types counts as 1 object, the array. While you're thinking about these things read (fellow UK User Group member) cubed2D's article on profiling too.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add