Android(iOS most likely as well): dalvikvm freeing memory = lag spikes

Hey guys I have been developing an endless runner game for the past month and development has been great and device performance as well except for one thing… garbage collection.

In my app I basically have pre-spawned obstacles, powerups that loop through the screen to creat an endless illusion. Performance is great, most frames take under a millisecond to compute, and about 25% others are a milliseconds or two. However every 10 - 20 seconds there is this very small stutter which is very annoying.

Upon building my app, connecting it to my PC and running ddms(an android debug utility that comes with android sdk) I noticed that the dalvik vm was freeing objects and memory every 10 - 20 seconds causing delays of 30 - 80 milliseconds. My app is set to run at 60 fps so these delays are HUGE, but even at 30 fps they would be noticeable. I am not spawning ANY new display objects, listeners, or tables, I am simply reusing all objects that I made in “createScene” and changing variables that represent game states and properties.

Since I am using storyboard I made sure to use forward declarations to make all functions, display objects, and tables local and not global.

I would really like some assistance as I have read the several memory leak related articles by ansca and other members.

Note: not using physics library [import]uid: 118482 topic_id: 28360 reply_id: 328360[/import]

Lua does automatic garbage collection but you can change the rate of it’s collection.

Try this:

collectgarbage( "setstepmul", 100 )  

Or if that doesn’t do the trick you could always stop the collector and manually collect garbage when it suits:

collectgarbage( "stop" )  

Let me know if either of these helps

More info: http://www.lua.org/manual/5.1/manual.html#2.10 [import]uid: 84637 topic_id: 28360 reply_id: 114752[/import]

Thanks for the reply ! I never new about that 1st option so I’l try both and get right back to you! [import]uid: 118482 topic_id: 28360 reply_id: 114994[/import]

Yes the stepmult is working beautifully!!! I assume if my lua memory is never above 700kb that is fine for almost all androids and iphones out there? Very exited to purchasing corona and releasing my first mobile game late this week! [import]uid: 118482 topic_id: 28360 reply_id: 114995[/import]