- Home /
What's causing scene to slow down?
I've started on a new project in which the level geometry is instantiated randomly/procedurally.
Objects are created beyond camera view and when they pass behind the player they are destroyed.
On the iPhone the game starts smoothly at around 40fps but there is a gradual slow down. After two minutes the scene is really chugging.
I'm wondering what is causing the deterioration in speed? I've double checked that every clone is getting destroyed once out of sight and the game reaches a maximum of about 60 objects and handles this fine initially.
Could this be something to do with instatiate and destroy on a constant basis? In my previous game, Surveillant I used these functions only occasionally.
Certainly sounds like something is getting stuck in memory... have you tried running the level with out the instancing?
I don't know what's causing this gradual slowdown, so I can't help you there. But for what you are describing I'd simply use procedural mesh generation ins$$anonymous$$d of instantiating objects.
Don't think using procedural mesh generation for this will work as I need every object to be a separate item with a rigidbody attached.
Answer by Recluse · May 23, 2011 at 03:39 PM
I've just built an alternate version, where all the objects are instanced on startup - no destroy or instantiate takes place during play. It runs at a constant rate without any slowdown. This leads me to believe using instantiate/destroy every update is causing the slowdown. Seems a shame, as instantiate/destroy worked fine to generate my random landscape. However I reckon I can write a work-around to achieve the same effect without using the two functions.
Anyone know why instantiate/destroy causes this gradual slow down exactly or how to patch it?
When you instantiate, you're creating an object from memory. when you destroy it, you arent actually destroying it from memory, you're just flagging it that you dont need it anymore, the garbage collect is what actually frees up the memory. If you are constantly creating and destroying every update then your heap memory is going to go up and up, and the garbage collector is probably not going to run on it's own until you use up your allocated memory, which is going to start causing your game to lag.
Thanks for that info - very helpful. Should I try garbage collecting manually? I think the way to go is with instantiating everything at the start and then shuffling the deck so to speak.
Yeah you could try calling it manually, but that wont guarantee a collection. Calling the GC manually just acts as a suggestion that it should collect. I agree that the way to go would probably be to instance everything at the start and rearrange it. In my own projects I almost never have to manually call the GC and just let it run automatically when a new level is loaded
I agree, instantiate/destroy seems to cause a lot of problems speed-wise. I figure I will instance everything on the start and then as objects fall behind the player I will place them upfront again just out of view and change their attributes/positioning etc. That way I get a constant speed rate throughout the game.