Skip to main content
Known Participant
July 27, 2013
Question

Fundamental Memory Question

  • July 27, 2013
  • 1 reply
  • 419 views

Dear developers, I am running into a garbage collection issue only in testing on IPAD 4 retina display (IPAD mini, along with Samsung Tab 10.1......no issues at all).  The issue is that when garbage collection triggers, it is causing a lag in gameplay for sometimes up to 4 to 5 seconds.  I am trying to avoid GC collection during game play altogether.

I know I can fix this but do not know where to begin.  Fundamentally, I believe the way I start a level is incorrect.  I have gone through all the adobe material and other websites too, but am not sure what I can do to decrease the lag.  This is the typical strategy I use when starting a level:

1.  Declare all arrays using "new" (they typically hold a lot of numbers).....I have a lot of arrays as I have a lot of moving elements that rotate.

2.  Load up all my bitmaps and mcs, again using the term "new".

3.  During the level, I do not remove any child.

4.  My game involves asteroids, but even though I may have as many as 48 entering the gameplay, I reuse them and only have 12 that are recycled.  I do this with my missiles as well, and any other object that makes multiple appearances.

5.  Once the level is completed, during the statistics portion where I tell the user some info about their performance, I call the  pauseforGCIcollection set at 0.25

6.  When either restarting the level or going to another level....I remove all the children, listeners (etc), null the variables ONLY for the mcs and bitmaps (not the arrays).  If I am returning to the menu page, I again call the pauseforGCIcollection.......if I am retrying the same level or going to another level, I do not call the pauseforgcicollection and go back to step 1.

About 40% of the times, I get this lag on IPAD 4 retina during game action.  On the mini, very rare.....on the Samsung....have never noticed it.

I do not mind putting in whatever effort I need to make this right for the IPAD 4, but have run out of tricks....if it is a fundamental change I need to make to steps 1 through 6, any suggestions you have would be much appreciated. 

I think the issue I am having is that it is not clear to me how memory is managed.  For example, with the arrays....do I need to null those too even if I am using the same arrays again when restarting the level and placing new elements into them?

Thank you,

Justin

This topic has been closed for replies.

1 reply

zeh
Inspiring
July 27, 2013

My thoughts, which are more general rather than iPad4-specific (I do admit it's strange you're only having problems on that device):

* Since you're using AIR, you can also call System.gc() to force a complete GC regardless of imminence value.

* The golden rule stands: try to never create anything during the game loop. It appears you're doing it right, using object pools and everything. But just thought I'd mention it.

* Check if you are not allocating too many small things that will be discarded later. These include even vars for Numbers and ints that you use for functions. You'd be surprised at the impact a bunch of numbers being constantly recreated and discarded can have in GC (even if not in total memory use). Can you just create some of those as class members (private "temp" vars) and just reuse them? And do you recreate too many objects when recyling your main game objects?

* 4-5 seconds is too much. Are you sure it's just GC? Have you tried testing with Adobe Scout? If not, this is the very first thing I'd try. It'll give you a better understanding on what's really taking up performance.

Known Participant
July 27, 2013

Thank you for your response.  I was under the impression that system.gc() does not really fire when I call it directly.  I have read about some hacks that can be done to try to get it to fire though.

You are right though, I do allocate a lot of small things but at the very first frame only, when I create all my variables.  During the level, I just give these variables their initial value.  These variables are all global.....when I first started coding I did not use any classes contrary to best programming practice.....partly because I was so excited about getting the levels to work that I did not want to invest the time.  This is something I may have to go back and do now....now that the game is almost ready for release :-(

I will check to see with Adobe Scout if it is only garbage collection.....but it is random thoughout the level and when it happens, it only happens once (never twice during gameplay in any level).  I have orbitting objects around the earth and can tell because the objects stutter a bit.  Even when there is very little happening in terms of gameplay action.  The same stuttering happens when I call the gc via the imminence parameter at level end...but not as noticeable.

Thank you,

Justin