Memory usage problem

So I have a little problem with memory usage on android (can’t test ios now so not sure about iphone). I’m using tilesheet.drawTiles on every frame. Memory usage goes to ~15 mb, drops to ~13 mb (gc?) then withing 10 seconds it goes again to ~15 mb, drops to 13 and it goes like that forever. Is that normal behavior? I mean everytime it goes down to 13 there is a small hiccup and fps drops to 27-28. Is there any way to avoid those hiccups? Here is what I’m doing on enterframe, I’m drawing around 120-150 objects at the same time:

[CODE]
renderSprite.graphics.clear();
renderArray.splice(0, renderArray.length);
var numChildren:Int = children.length;
var i:Int = 0;

while (i < numChildren)
{
var child:ZSprite = children[i]; //ZSprite contains data on position, matrix, and tile id
if (child.visible)
{
renderArray.push(child.x);
renderArray.push(child.y);
renderArray.push(child.currentFrame);
#if neko
child.resetMatrix();
#end
var matrix:Matrix = child.matrix;
renderArray.push(matrix.a);
renderArray.push(matrix.b);
renderArray.push(matrix.c);
renderArray.push(matrix.d);
child.update();
i++;
}
}
tilesheet.drawTiles(renderSprite.graphics, renderArray, SMOOTHING, Tilesheet.TILE_TRANS_2x2);
[/CODE]

splice() returns a new array containing all the removed values, even if you don’t need it. This could account for some of the extra memory usage.

Instead of splice(), try setting renderArray.length = 0.

Cannot access field or identifier length for writing :confused:

If I do this:

renderSprite.graphics.clear();
 if(renderArray.length > 0)
 {
            tilesheet.drawTiles(renderSprite.graphics, renderArray, SMOOTHING, Tilesheet.TILE_TRANS_2x2);
            return;
 }
 renderArray.splice(0, renderArray.length);

Memory usage doesn’t go up so I guess splice is really causing this problem…what can I do about this? How should I clear my array?

UPDATE
eh, it seems that this is actually caused by Tilesheet.TILE_TRANS_2x2.
This works fine:

tilesheet = new Tilesheet(bitmap.bitmapData);
    tilesheet.addTileRect(bitmap.getBounds(this));
    this.addEventListener(Event.ENTER_FRAME, ef);
    private function ef(e:Event):Void
    {
        graphics.clear();
        tileData.splice(0, tileData.length);
        for(i in 0...100)
        {
            tileData.push(0);
            tileData.push(0);
            tileData.push(0);
        }
        tilesheet.drawTiles(graphics, tileData, true);
    }

But this makes the memory go crazy

 tilesheet = new Tilesheet(bitmap.bitmapData);
    tilesheet.addTileRect(bitmap.getBounds(this));
    this.addEventListener(Event.ENTER_FRAME, ef);
    private function ef(e:Event):Void
    {
        graphics.clear();
        tileData.splice(0, tileData.length);
        for(i in 0...100)
        {
            tileData.push(0);
            tileData.push(0);
            tileData.push(0);
            tileData.push(1);
            tileData.push(0);
            tileData.push(0);
            tileData.push(1);
        }
        tilesheet.drawTiles(graphics, tileData, true, Tilesheet.TILE_TRANS_2x2);
    }

This seems like a pretty big flaw, Could someone test this out? Maybe it has something to do with my libs?

update 2
Other flags cause this as well : (

drawTiles can take a parameter which tell how many elements of the array to use, this way you won’t have to empty it and reallocate memory each frame.
http://docs.openfl.org/openfl/display/Tilesheet.html#drawTiles

1 Like

Yeah, my first guess would be all the push calls and GC when you allocate a new array

Yup, eliminating splice and pushes on every frame fixed this :slight_smile:
Android version runs better however there is no performance difference on flash which is kind of weird I guess…but it’s not really an issue. Thanks everyone :smile:

Depending on your device, Flash might be running on something much more powerful (and with more RAM) than your Android device. That could account for why it’s a negligible difference in Flash… Or maybe not.