I'm porting a Flashgame to iOS. I already cpu-, render- and memory-optimized it and it runs really smooth on the device, EXCEPT for some very serious lags that always happen at the same time in the first level.
Here is a video showing the lags (sorry for bad quality, but the lags are very apparent):
I keep track of memory consumption over the runtime of the game, here is a screenshot:
(click for higher resolution)
the blue line shows System.totalMemory
the green line shows System.freeMemory
the yellow line shows System.privateMemory
all lines are scaled in respect to their Peak value
the red lines show lags (black meaning the frame runs at the desired framerate and the red is something like reddishness = (1.0-fps/Stage.frameRate)*red), but only two of those lags are really relevant (marked in the screenshot).
at one of the lags, there is a very large drop in System.totalMemory, but only for a very short time (this spike always occurs, but randomly either on the first or on the second lag. i never observed it to happen on both lags and i never observed it to happen on neither lag)
The firing of the garbage collector is also visible in the graphic, but it does not seem to cause any lag - however it affects only System.totalMemory as well as System.freeMemory. System.privateMemory seems to be unaffected.
Manually calling the garbagecollector also doesn't solve the problem. in the following screenshot i make manual calls to System.gc() (visible as cyan collored lines) - and again it affects only System.totalMemory and System.freeMemory, but not System.privateMemory:
(click for higher resolution)
I need help in tracking down the problem. Does anyone have a good understanding in what exactly System.privateMemory is and what could cause the significant drop on the lags? also It's weird that the drop of System.privateMemory is always apparent on only one of the two lags, but never on both.
Any help on that is highly apreciated!
The lags appear to happen at regular time intervals. if i make my character run at half the speed, there will be twice as many lag during level 1 and the memory usage will look like this:
Sorry, I don't have any real insight, but I wonder if it has to do with sending graphics to the GPU/texture memory? This has caused bottlenecks in the past for us. However, in your case running the character at half the speed should not have caused more lags if this was the case.
Have you also tried compiling with a different version of the AIR SDK? We just recently had a memory problem (random crashes) that was solved simply by using v3.1 (we were on 2.7), though for you I wonder if trying an older version (like 2.7) may resolve it?
Actually, by now i'm convinced that exactly this was the problem. I noticed that if my 3D Container (the surrounding) contained only the backwall, then the backwall would become pixelated as it comes close to the character - however, the lags were gone. But as soon as i added rotated movieclips into this container (the walls, floor and ceiling in this case), then the backwall would not become pixelated anymore - so, for some reasons in this case the runtime was updating the texture on the GPU at some point (with no control for me over this behaviour).
My solution was to completely abstain from Matrix3D objects. Instead i wrote specialized Classes for MovieClip, Sprite and Bitmap which override the setters and getters of theyr base classes' 3d properties and instead of setting a Matrix3D propperty, do the 3D math internally and simply set .x, .y, .xScale and .yScale. This works like a charm, but of course only for camera facing objects. for the rotated objects I used drawTriangles instead with properly calculated UV coordinates.
Now the lags are gone. But it would be really cool if 3D properties could be used with more control over the GPU interaction. Maybe with Starling