Game Development Community

Strings, Components and Garbage Collection

by Henry Shilling · in Torque X 2D · 03/18/2010 (1:33 pm) · 4 replies

I was still having some issues with little glitchy jumps in my game. Decided on the advice of some of you guys to get into the memory and profiling tools.

Strings are pretty bad overall and generate garbage for the collector If you want to read more check out Shawn Hargreaves blog posts in my earlier frame rate jumping thread.

What I found was that cloning any object creates some strings, no matter what you do, you will get at least 4 string in each component. This is apparently important stuff they're the #1 and #2 entries in the XNA remote performance monitor. There is no way around it, without radically changing a lot of the Torque Engine, without them you could not look up a component by name. So now each object with components will generate at min 4 strings of some length. My bullets have 4 components, Physics, Collision, World Limit, and the ability to do damage. 16 strings of varying length each one generating about 2k of data that must be dealt with by the GC. The GC will clean memory at 1M. This means about 500 bullets and the GC hits.

Not to bad so far, but I have a gun that shoots 5 bullets at a time 10 times a second. in 10 secs I hit the garbage collector. Then the enemies shoot at me. Plus the enemies also have various components, I think you see where I am going.

This is something to consider when building components, if you are going to have lots of things on screen, pay attention to how many components you have attached.

#1
03/18/2010 (5:47 pm)
Also try using pooled components and objects. i.e. they get recycled rather than getting cloned, so (in theory) nothing to garbage collect.
#2
03/18/2010 (8:04 pm)
The built in pool went real bad real fast. A few minutes and bam, 1FPS and worse.

I love stacking simple behaviours, they are like legos, I'll keep all my small behaviours and use them for testing out ideas and when I need efficiency I'll build a bigger component.
#3
03/18/2010 (9:23 pm)
This is a hacky solution but it might work.

What if rather then destroying the bullets you created say 100 of them at the beginning of the game off screen with collision disabled and slotted them into an array

then when fireing your guns you could cycle through the array to select the bullets and move them to the proper location re-enabling the collision.

When the bullet hit you would just place it back in a holding bin off screen with collision disabled.


As a side note I'm surprised your having problems with the pooling. I've been cloning the crap out of animated sprites (as particles cant have animated textures) though I usually only have one component on them.

Are you updating strings by any chance? at one point I had a memory counter to find any potential leaks. It updated a string every frame and totally trashed my performance.
#4
03/19/2010 (12:30 am)
Matt your right, it really didn't make very much difference, I am really getting interested in what Pino has going on there. When I first ran my stuff through the ANTS memory profiler I noticed the biggest thing in memory were the particle effects. They also seem to really grow in less than a minute I went from 43 to 350 particle effects in memory. Like they are not getting tossed.

Still I am not experiencing a terrifying slowdown, just enough to annoy me.

The funny thing about the strings, if you have any GUIText on screen it's generating a string each frame, I put up a screen and watched it count off 60 strings generated per frame, nothing on screen but a gui with one GuiText that was just static.